Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@synthesize vs @dynamic, what are the differences?

What are the differences between implementing a @property with @dynamic or @synthesize?

like image 274
nico Avatar asked Jul 21 '09 17:07

nico


People also ask

What is @dynamic in Objective-C?

@objc means you want your Swift code (class, method, property, etc.) to be visible from Objective-C. dynamic means you want to use Objective-C dynamic dispatch.

What is @dynamic in Swift?

Swift and Objective-C Interoperability Dynamic what? Dynamic dispatch. It simply means that the Objective-C runtime decides at runtime which implementation of a particular method or function it needs to invoke.

What is Objective-C synthesize?

@synthesize tells the compiler to take care of the accessor methods creation i.e it will generate the methods based on property description. It will also generate an instance variable to be used which you can specify as above, as a convention it starts with _(underscore)+propertyName.

What is synthesize in Swift?

Synthesized/auto-synthesized properties in Objective C -- these are called "stored properties" in Swift. You simply declare it with var topSpeed : Double or let topSpeed : Double = 4.2 in a class declaration, exactly as you would declare a local variable in a function body.


2 Answers

@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 or will be provided at runtime).

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.

@dynamic also can be used to delegate the responsibility of implementing the accessors. If you implement the accessors yourself within the class then you normally do not use @dynamic.

Super class:

@property (nonatomic, retain) NSButton *someButton; ... @synthesize someButton; 

Subclass:

@property (nonatomic, retain) IBOutlet NSButton *someButton; ... @dynamic someButton; 
like image 130
diederikh Avatar answered Oct 04 '22 03:10

diederikh


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

like image 45
Alex Rozanski Avatar answered Oct 04 '22 02:10

Alex Rozanski