objective c - What is the difference between this 2 @synthesize Pattern and which is Recommended? -


many places in sample code have seen 2 different way of @synthesize variable. example here taking 1 sample button. @property (strong, nonatomic) iboutlet uibutton *logonbutton;

1.@synthesize logonbutton = _logonbutton;

2.@synthesize logonbutton;

among 2 methods 1 recommended?

short answer

the first method preferred.

long answer

the first example declaring generated ivar logonbutton property should _logonbutton instead of default generated ivar have same name property (logonbutton).

the purpose of prevent memory issues. it's possible accidentally assign object ivar instead of property, , not retained potentially lead application crashing.

example

@synthesize logonbutton;  -(void)dosomething {     // because use 'self.logonbutton', object retained.     self.logonbutton = [uibutton buttonwithtype:uibuttontypecustom];       // here, don't use 'self.logonbutton', , using convenience     // constructor 'buttonwithtype:' instead of alloc/init, not retained.     // attempting use variable in future invariably lead      // crash.     logonbutton = [uibutton buttonwithtype:uibuttontypecustom];  } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -