@dynamic


#1
@implementation BNRItem

@dynamic dateCreated;
@dynamic itemKey;
...

From the generated BNRItem.m, I see some lines with ‘@dynamic.’ Could anyone explain what they are? Thanks.


#2

For properties, you’ll either see @synthesize or @dynamic.

@synthesize is what happens automatically. When you use synthesize, it creates the getter and setter for you as well as the backing instance variable, all at compile time. (The exception to this is if you implement your own getter and setter, then the ivar is not created for you.)

@dynamic means that the getter/setter will be dynamically created at run time, and no backing ivar will be created. It is basically telling the compiler: “don’t worry about the lack of these methods, I promise that they will exist by the time the application needs them.” In the case of Core Data, it is because the accessors tie into the underlying storage mechanism (SQLite, for example) which are not instance variables.

This is all possible due to the dynamic nature of Objective-C. Although we rarely do so explicitly ourselves, you are able to do a whole bunch of things at run time such as added new classes, adding new methods to existing classes, changing the implementation of methods, etc. So in this case, Core Data is adding those methods for you at run time.