@dynamic

1. @dynamic到底是什么意思?

在cocoachina看到过对于该变量的解释:

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. For example:

// Movie.h
@interface Movie : NSManagedObject {
}
@property (retain) NSString* title;
@end 


// Movie.m
@implementation Movie
@dynamic title;
@end 



The other subtle note here is that there's no title instance variable defined. This is because NSManagedObject has its own data store. Since we're using @dynamic , there will be no warnings or errors about the missing instance variable.

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

2. 和@synthesize的区别:

@dynamic :是开发者自已提供相应的属性声明:对于只读属性需要提供 setter,对于读写属性需要提供 setter 和 getter。

@synthesize:是系统自动生成getter和setter属性声明。除非开发人员已经做了,否则由编译器生成相应的代码,以满足属性声明 

附带一个例子:http://blog.csdn.net/sodaslay/article/details/7350876

你可能感兴趣的:(@dynamic)