Vitaly Ishkulov
Vitaly Ishkulov

Reputation: 11

What UIView setter does @synthesize create

How to make custom UIView setter. For example:

1) We create property:

@property (retain) IBOutlet UILabel *myLabel

2) we make setter (the same as @synthesize would create):

- (void)setMyLabel:(UILabel *)anObject
{
     [myLabel release];
     myLabel = [anObject retain]; 
}

Is it correct, or should I check if the new view are not the same as the current with

- (void)setMyLabel:(UILabel *)anObject
{
    if(anObject != myView){
        [myLabel release];
        myLabel = [anObject retain]; 
    }
}

Just myView and anObject are object pointers. So should we check them with -isEqual method then? Or we don't need to check it at all? What code does @synthesize generates by defaults?

Thanks.

Upvotes: 1

Views: 670

Answers (1)

Ole Begemann
Ole Begemann

Reputation: 135578

Only the second version (with the if statement) is correct. In your first version, imagine that anObject and myLabel actually point to the same object (i.e., the pointers are the same). In that case, you would release the object, which would cause it to be deallocated if no other object had retained it. The subsequent attempt to retain the deallocated object would cause a crash.

Upvotes: 2

Related Questions