-1

What happens if I create a property with "assign" attribute set the property to nil in dealloc method

@property (nonatomic, assign) NSString* myData;

- (void)dealloc {
    self.myData = nil;
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309

2 Answers2

3

then the setter is called which just sets the pointer to nil and nothing else happens.

edit: difference between (nonatomic, assign) and (nonatomic, retain)

An assign-property will only set the pointer and a retain-property will also call release on the old and retain on the new object.

The synthesized (nonatomic, assign)-setter will look like this:

-(void) setMyData:(NSString*)value
{
    myData = value; //just assigning the pointer
}

And the synthesized (nonatomic, retain)-setter will look like this:

-(void) setMyData:(NSString*)value
{
    [value retain];   // retain new object
    [myData release]; // release old object. if myData is nil: nothing happens
    myData = value;   // assigning the pointer
}

Between the getters there is no difference. Both are just nonatomic.

thomas
  • 5,637
  • 2
  • 24
  • 35
  • Normally we do @property (nonatomic, retain) NSString* myData; Then what is the difference here. – Abhinav Oct 13 '11 at 21:00
0

There is nothing wrong with doing that.

There is normally no benefit to doing that. It will have no effect, unless some more code in dealloc runs after that that depends on the value of myData.

When you set a property declared and defined as retain to nil, it causes release to be sent to the previous property value. But when it's defined as assign, that doesn't happen. It basically just sets the instance variable, which normally doesn't matter in dealloc since nothing else is going to read the value of the instance variable.

morningstar
  • 8,952
  • 6
  • 31
  • 42