gyozo kudor
gyozo kudor

Reputation: 6336

Why don't properties get automatically synthesized

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.

Upvotes: 4

Views: 1717

Answers (5)

Nathan
Nathan

Reputation: 12294

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

Upvotes: 0

Richard
Richard

Reputation: 3376

@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.]

Upvotes: 0

bbum
bbum

Reputation: 162712

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.

Upvotes: 14

diederikh
diederikh

Reputation: 25271

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

Upvotes: 1

Vladimir
Vladimir

Reputation: 170819

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)...

Upvotes: 2

Related Questions