Reputation: 30618
For example, I have a UIViewController, and have a value:
NSString *testString;
@property (nonatomic, retain) NSString *testString;
in the .m:
@synthesize testString;
And what is the different between these two lines of code:
testString = @"something";
self.testString = @"something";
Upvotes: 0
Views: 358
Reputation: 2227
in non-ARC code there is a significant difference if you're declaring properties as retain
or copy
. in these two cases, there's a bit more going on behind the scenes when you call self.property
. very simplistically, retain
does something like:
- (void)setFoo:(NSString *)newFoo {
[newFoo retain];
[foo release];
foo = newFoo;
}
and with copy it'd be something like:
- (void)setFoo:(NSString *)newFoo {
NSString *oldFoo = foo;
foo = [newFoo copy];
[oldFoo release];
}
}
Upvotes: 0
Reputation: 79870
The first case assigns new value to testString
, you are accessing testString
directly:
testString = @"something";
In the second case:
self.testString = @"something";
You are calling setter
generated by calling @synthesize testString
and the fact that it's a property.
The second case calls generated [aUIViewControllerObject setTestString: @"something"]
in which the @"something"
NSString
is retained (because the testString
property is declared with retain
parameter), the old value is autoreleased
or released
and the new value is assigned to testString
.
Upvotes: 1
Reputation: 6942
self.testString = @"something";
implicitly calls setTestString:
method. In your case it means following:
-(void) setTestString: (NSString*) newString
{
if (testString != newString)
{
[testString release];
testString = [newString retain];
}
}
Upvotes: 0