ios - Why use @synthesize statements -


when first learned ios programming (i believe pre-arc stanford lectures), synthesized properties this:

@synthesize myproperty=_myproperty 

however, appears has been default behaviour while. so, omitting statement exact same leaving in.

my question this: given default behaviour, practical examples we'd want use @synthesize statement?

with ios 6, llvm compiler came called "property autosynthesis". basically, means compiler add @synthesize every @property sees in following form:

@synthesize propertyname = _propertyname; 

that is, create ivar prefixed underscore , generate getter & setter using ivar.

there 1 reason use @synthesize explicitly , reason rename ivar or, example, use same ivar 2 different properties.

also note in cases need use @dynamic prevent synthesis. typically when know methods (getter or setter) defined somewhere compiler doesn't know them.

there no memory implications autosynthesis not connected arc @ all.

what if manually implement getter , setter?

if property readwrite , implement both setter , getter or if property readonly , implement getter, nothing synthesized. synthesis refers automatic definition of 2 methods. why should compiler synthesize there? if manual implementation needs ivar, declare ivar, don't need add @synthesize add ivar (although can).

edit

i forgot 1 reason when using @synthesize needed , when implementing property declared in protocol. such property won't autosynthesized. typical example [uiapplicationdelegate window].


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -