4

maybe this is a silly question. Every time I make a @property I have to @synthesize it. But this makes no sense the only thing you can do with a @property(whatever) Type* property is to do @synthesize property in the implementation file. So why are both needed? Why isn't the compiler generating the getter/setter methods automagically without me having to write @synthesize property.

gyozo kudor
  • 6,284
  • 10
  • 53
  • 80

5 Answers5

14

In the current production compilers, the default -- the case without @synthesize -- is to do nothing and then warn if an implementation isn't provided.

@synthesize is automatic in the latest versions of the LLVM 2.0 compiler.

@dynamic is not required when implementing the setter/getter yourself. @dynamic is used when dynamically providing the implementations at runtime. That is, @dynamic foo; combined with @property <type> foo; will cause the compiler not to warn if you don't provide a -foo and -setFoo: implementation.

Note that you can also use @synthesize propertyName = instanceVariableName; to use a specific, differently named, instance variable as the backing store.

@property in the interface very much is short hand for the getter/setter method declarations. It also carries more metadata (retain, assign, etc..) that is leveraged by the compiler during @synthesize.

And, as always, an atomic property doesn't really help with thread safety.

bbum
  • 162,346
  • 23
  • 271
  • 359
2

It is just historically so that current compiler requires that. In XCode 4 those @synthesize won't be required anymore (as per WWDC videos, hope I do not violate NDA here)...

Vladimir
  • 170,431
  • 36
  • 387
  • 313
1

@synthesize is not the only option. @dynamic property is also possible.

diederikh
  • 25,221
  • 5
  • 36
  • 49
0

As of Xcode 4.4 this is now what happens. Synthesize is no longer explicitly required.

Nathan
  • 11,938
  • 12
  • 55
  • 62
0

@synthesize is not the only option; there is also @dynamic, which means you will implement the methods yourself. [Redacted; see bbum's answer for more detail.]

Richard
  • 3,316
  • 30
  • 41
  • further discussion of the differences: http://stackoverflow.com/questions/1160498/synthesize-vs-dynamic-what-are-the-differences – Richard Nov 01 '10 at 12:24
  • Thanks. It still seems strange to me though. If I implement the getters and setters myself I can just copy the function definitions into the header files without using @property declaration. :) – gyozo kudor Nov 01 '10 at 12:35
  • This answer is generally correct, but gets a number of details wrong. See my answer. – bbum Nov 01 '10 at 15:36
  • You better not get a compiler warning if you add @dynamic! That is the whole point! (Checking) And, in fact, you do not get a compiler warning. Of course, you'll get a runtime exception if you don't dynamically inject an implementation. – bbum Nov 01 '10 at 18:19
  • @bbum I don't know what I was thinking when I wrote that (now-removed) comment. – Richard Nov 01 '10 at 20:55
  • No worries -- it seems to be a very common point of confusion. I'm curious as to where it is coming from. – bbum Nov 01 '10 at 21:16