Thanks
Thanks

Reputation: 40329

What happens exactly, when I set an property to nil in the dealloc method?

Example

-(void)dealloc {
    self.myOutletProperty = nil;
    [super dealloc];
}

I guess that a sort-of virtual setter will be called. But what exactly happens here? Why nil?

Upvotes: 1

Views: 2618

Answers (4)

Marc Charbonneau
Marc Charbonneau

Reputation: 40517

One thing to keep in mind is that even though setting your property to nil will work fine, I recommend calling [object release] in your dealloc method instead. This way you're safe if you write our own setter method that references another ivar (which may have already been released) or you have KVO notifications registered on that property somewhere else.

Upvotes: 2

Igor
Igor

Reputation: 1258

You should know, that property is just syntax sugar.

for example:

@property(nonatomic, retain) NSString *myString;

will convert to

- (NSString*)myString { 
    return myString; 
}

- (void)setMyString:(NSString*)newString {
    if (myString != newString) {
        [myString release];
        myString = [newString retain];
    }
}

so if you declare @property in than way, it actually releasing

Upvotes: 12

oxigen
oxigen

Reputation: 6263

1.If property type is "copy" or "retain", then

self.myOutletProperty = nil; is the same as [myOutletProperty release];

2.If property type is "assign", then

self.myOutletProperty = nil; do nothing

Upvotes: 7

Ilya Birman
Ilya Birman

Reputation: 10072

Nil is the same as null, but for objects. It means no object.

Dot syntax is same as calling [self setMyOutletProperty:nil].

So you just remove an object from some property. Meaning depends on what property you are talking about.

Upvotes: 1

Related Questions