Tuesday 22 April 2014

@synthesize vs @dynamic

@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:




Super class:

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


Subclass:

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


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










2 comments:

  1. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
    "

    ReplyDelete
  2. Articles, while primarily text-based, may also include visual aids such as charts, graphs, Password Protect A PDF For Free or illustrations to support the information presented.

    ReplyDelete