views:

2472

answers:

3

what are the differences between implementating a @property with @dynamic or @synthesize??

thanks.

+3  A: 

Take a look at this article; under the heading "Methods provided at runtime":

Some accessors are created dynamically at runtime, such as certain ones used in CoreData's NSManagedObject class. If you want to declare and use properties for these cases, but want to avoid warnings about methods missing at compile time, you can use the @dynamic directive instead of @synthesize.

...

Using the @dynamic directive essentially tells the compiler "don't worry about it, a method is on the way."

The @synthesize directive, on the other hand, generates the accessor methods for you at compile time (although as noted in the "Mixing Synthesized and Custom Accessors" section it is flexible and does not generate methods for you if either are implemented).

Perspx
This is morer-correcter man. This answer is the only answer that talks about methods created at runtime, which really seems to capture the spirit a lot more than top voted ans atm
bobobobo
+25  A: 

@synthesize will generate getter and setter methods for your property. @dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass)

Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to create an outlet for a property defined by a superclass that was not defined as an outlet:

Super class:

@property (nonatomic, copy) NSString *someString;
...
@synthesize someString;

Subclass:

@property (nonatomic, copy) IBOutlet NSString *someString;
...
@dynamic someString;
Diederik Hoogenboom
thanks man. Clear and fast answer.
nico
not 100% right; dynamic is the default if you don't set either @synthesize or @dynamic. specifying @dynamic merely means that you take responsibility for properly implementing the property accessors based on the signature of the property declaration.
Kevlar
Not really, @dynamic means to responsibility of implementing the accessors is delegated. If you implement the accessors yourself within the class then you normally do not use @dynamic.
Diederik Hoogenboom
I was getting `NSUnknownKeyException` errors with my dynamic property when I removed the `@synthesize` line (Xcode 3.2 was giving me an error b/c I had no matching ivar for my @property). Adding `@dynamic` fixed the issue - compiles and runs fine now. Thanks!
pix0r
+3  A: 

As others have said, in general you use @synthesize to have the compiler generate the getters and/ or settings for you, and @dynamic if you are going to write them yourself.

There is another subtlety not yet mentioned: @synthesize will let you provide an implementation yourself, of either a getter or a setter. This is useful if you only want to implement the getter for some extra logic, but let the compiler generate the setter (which, for objects, is usually a bit more complex to write yourself).

However, if you do write an implementation for a @synthesize'd accessor it must still be backed by a real field (e.g., if you write -(int) getFoo(); you must have an int foo; field). If the value is being produce by something else (e.g. calculated from other fields) then you have to use @dynamic.

Phil Nash
+1 for mention of important difference: @dynamic lets you create accessors for varaibles not defined in your class interface and through introspection.
mahboudz