There are a couple of problems.
init is not a void return method. Init is:
-(id)init;
because init you have to do special initialization and creation it would look more like:
Code:
-(id)init {
self = [super init];
if (self) {
self.prop = [NSArray arrayWithObjects:@"1", @"2", nil];
}
return self;
}
attempting to do (void)init will break your object quite nicely.
Now to explain the setting of prop... (all of this assumes you have either written the appropriate setter and getter for the property or @synthesized them) You will loose the object when the autorelease pool is drained (which in an iOS app is every cycle of the run loop).
when you do something like:
prop =
you're actually setting the ivar itself, you're not utilizing the setter that is related to the property (using the setter with the specification you have set would actually retain the returned value so even tho the autorelease pool draining would release the object, your instance of your class would still have a retain on it so it would not go away).
You would need to do either:
Code:
self.prop = [NSArray arrayWithObjects:@"1", @"2", nil];
OR
Code:
[self setProp:[NSArray arrayWithObjects:@"1", @"2", nil]];
To utilize the setter and properly retain the object you created.
synthesized properties are methods that are validly used with either the dot accessor OR the traditional way of invoking a method (the []). The dot accessor DOES NOT directly access the variable, it is just short hand for the setter (or getter) methods - the setter and getter methods are responsible for modifying the actual ivar.
If you didn't want to use the setter, and wanted to directly set the variable like you did - but wanted to make sure the data didn't vanish you could:
prop = [[NSArray arrayWithObjects
"1", @"2", nil] retain];
Keep in mind, the nice thing about using the setter (setProp: or the dot accessor (ie: self.prop = ) ) is that it will set the new object and release the old. If you were to in a later method do:
prop = [[NSArray arrayWithObjects
"3", @"4", nil] retain];
assuming you didn't release the original value, you would now have a memory leak as your ivar would be pointing to this new object and no longer have a reference to the original one you created in init.