iPhone Dev - @synthesize keyword?
I am learning iPhone development. In the book examples, there is @synthesize keyword is mentioned for properties.
For a control, I define property in .h file but NOT @synthesize in .m file. I am accessing to property of text box with .text attribute. Also I have linked outlets of a text box, and text box's name is different than the property name.
And code run开发者_如何学Cs fine; so is @synthesize keyword not needed? and When?
From Apple's docs > Mac Dev Center > Cocoa Core Competencies > Declared Property:
In addition to declaring the accessor methods, you can instruct the compiler to synthesize implementations of them (or inform the compiler that your class will synthesize them at runtime).
You use the @synthesize statement in a class’s implementation block to tell the compiler to create implementations that match the specification you gave in the property declaration.
for reference:
You use the @dynamic statement to tell the compiler to suppress a warning if it can’t find an implementation of accessor methods specified by an @property declaration.
The @synthesize directive controls the creation of accessor methods. However, you don't strictly speaking have to use accessors methods, they're just such really good idea that it pays to make the complier generate them automatically.
Accessor methods give fine tuned control over variables and make key-value coding easier.
Prior to Objective-c 2.0, you had to write accessors manually. That meant writing two methods for every variable. It was tedious and a lot of people used scripts to do it. When they updated the language, they just included those scripts. The @property, @synthesize and @dynamic compiler directives activate those scripts.
精彩评论