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