tags:

views:

242

answers:

2

Hi, 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 runs fine; so is @synthesize keyword not needed? and When?

+2  A: 

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.

Justin
ok but it seems that i have done neither
paul simmons
@paul simmons Then the (likely cases) methods declared by the property are implemented in the m file, or the compiler warns you about it
Justin
ah, is it just a warning? not an error?
paul simmons
@paul simmons wouldn't it make more sense to tell me what you're seeing that is specifically different from what I've written, rather than making me sanity check what I've written? right now, I can't tell whether you tried it (or not), or if you are seeing an error. If you are seeing an error, I certainly can't read it until you post it. Even if it were an error, you may have different build/toolchain/analysis settings, since errors and warnings alike should be corrected... Having said that: What have you tried (specifically), and what are you seeing that is different (specifically)?
Justin
+1  A: 

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.

TechZen