self.property = value;
is equal to:
[self setProperty:value];
That is to say, the declaration using self.
goes through the object's accessor method, rather than using direct access.
They have different causes and effects. Perhaps the most notable is that direct access will often leads to reference count issues (leaks/zombies) if not used with care. The accessor is responsible for handling memory management - if synthesised, or if you implement it yourself.
The General Rule: You should favor using the accessors (self.blah = thing;
) over direct access (blah = thing;
) until you know when and why you would make exceptions to this rule.
The immediate exception: There is one exception to the general rule: Do not use the accessors in partially constructed states, such as the object's initializer or dealloc
. In those cases, use direct access:
- (id)init
{
self = [super init];
if (0 != self) {
things = [NSArray new];
}
return self;
}
- (void)dealloc << not needed with ARC, in this case
{
[things release], things = 0;
[super dealloc];
}
Update
Describing Bavarious' suspicion of the error:
It sounds like you have declared an instance variable, but have not declared an associated property or proper accessors (e.g. the setter). Here's a breakdown of a class' declaration with ivars and properties:
@interface MONObject : NSObject
{
@private
NSMutableArray * myMutableArray; << declares an instance variable
}
// the property declaration:
@property (nonatomic, retain, readwrite) NSMutableArray * myMutableArray;
// adds the accessors:
// - (NSMutableArray *)myMutableArray; << getter
// - (void)setMyMutableArray:(NSMutableArray *)arg; << setter
// to the class' interface.
@end
@implementation MONObject
// @synthesize below generates the accessors for the property
// myMutableArray, using "myMutableArray" ivar by default.
@synthesize myMutableArray;
- (void)zumBeispiel
{
NSUInteger count = 0;
// direct access:
count = [myMutableArray count];
// is equal to:
count = [self->myMutableArray count];
// Access via the getter:
count = [self.myMutableArray count]; << equal to [self myMutableArray]
// is equal to:
count = [[self myMutableArray] count];
}
@end