@synthesize 与 @dynamic,有什么区别? [英] @synthesize vs @dynamic, what are the differences?
问题描述
实现 @property
和 @dynamic
或 @synthesize
有什么区别?
What are the differences between implementing a @property
with @dynamic
or @synthesize
?
推荐答案
@synthesize 将为您的属性生成 getter 和 setter 方法.@dynamic 只是告诉编译器 getter 和 setter 方法不是由类本身实现的,而是其他地方(如超类或将在运行时提供).
@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).
@dynamic 的用途是例如使用 NSManagedObject
(CoreData) 的子类,或者当您想为未定义为插座的超类定义的属性创建插座时.
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 也可用于委派实现访问器的责任.如果您在类中自己实现访问器,那么您通常不会使用@dynamic.
@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.
超类:
@property (nonatomic, retain) NSButton *someButton;
...
@synthesize someButton;
子类:
@property (nonatomic, retain) IBOutlet NSButton *someButton;
...
@dynamic someButton;
这篇关于@synthesize 与 @dynamic,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!