synthesize 与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)
 

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)IBOutletNSButton*someButton;
...
@dynamic someButton;
 
1:通过@synthesize 指令告诉编译器在编译期间产生getter/setter方法。
2:通过@dynamic指令,自己实现方法。@dynamic 就是要告诉编译器,代码中用@dynamic修饰的属性,其getter和setter方法会在程序运行的时候或者用其他方式动态绑定,以便让编译器通过编译。其主要的作用就是用在NSManagerObject对象的属性声明上,由于此类对象的属性一般是从Core Data的属性中生成的,core data 框架会在程序运行的时候为此类属性生成getter和setter方法。
posted @ 2016-08-10 14:28  imKirin  阅读(332)  评论(0编辑  收藏  举报