correct property initializing

Joined
Oct 4, 2011
Messages
6
Reaction score
0
Points
1
If we initialize class property, created using
Code:
@property (nonatomic, retain) NSArray *prop
in the body of function
Code:
(void)init
in this way
Code:
prop = [NSArray arrayWithObjects:@"1", @"2", nil]
where will be a mistake in this code. What consequences in working program it will have and in what time, in what function it will happen?
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
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:mad:"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:mad:"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.
 
OP
S
Joined
Oct 4, 2011
Messages
6
Reaction score
0
Points
1
Nethfel, thank you so much for your responsiveness, your answers were very very helpful!!!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top