IOS-synthesize和dynamic的异同


一、SDK中描述是在声明property的时候,有2个选择
1:通过@synthesize 指令告诉编译器在编译期间产生getter/setter方法。
2:通过@dynamic指令,自己实现方法。
有些存取在运行时动态创建CoreDataNSManagedObject使用的某些如果你想这些情况声明和使用属性,但要避免缺少方法在编译时警告可以使用@dynamic动态指令不是@synthesize合成指令例如
@interface Demo : NSManagedObject {
}
@property (retain) NSString* test;
@end

@implementation Demo
@dynamic test;
@end

二、

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM