1、在protocol中也是可以使用property的剑令,先看代碼
#import <Foundation/Foundation.h>
@protocol DataManageDelegate <NSObject>
@property (nonatomic, copy) NSString *identifier;
@end
在ViewController中遵循了該協(xié)議之后,然后給identifier
賦值self.identifier = @"123";
谨究,運行工程莹汤,發(fā)現(xiàn)crash了,Xcode提示是:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController setIdentifier:]: unrecognized selector sent to instance 0x7f8ad4f069e0'
读第,意思就是沒有實現(xiàn)identifier的setter方法曙博。getter方法沒實現(xiàn)同樣會crash。
2怜瞒、解決方法:
加上@synthesize identifier = _identifier;
父泳,讓編譯器自動生成setter和getter方法。這樣就不會crash了
3吴汪、@dynamic是讓系統(tǒng)不要自動生成setter和getter方法惠窄,如果使用了@dynamic但是沒有自己實現(xiàn)setter和getter方法,賦值或者取值的時候就會crash漾橙,這個比較少用杆融。