
本文共 4099 字,大约阅读时间需要 13 分钟。
���������������������
���Objective-C���������������������������������������������������������������������������������������������getter���setter���������������������������@synthesize
���������������������������������������������������������������������������������������������������������������
���������PersonProtocol
���������������������work
���������getter���setter���������������������������������������������������Student
������������Person
������������������������������work
������������������������������������������
- ������:
Use of undeclared identifier '_work'
- ������:
Auto property synthesis will not synthesize property 'work' declared in protocol 'PersonProtocol'
���������������������protocol���������������������������������������������������������������������������������������������������������������@synthesize
������������������������������������������Student
������������@synthesize work = _work;
���������������������_work
���������������
������@synthesize
������Student
������������������������
- (void)setWork:(NSString *)newWork { _work = newWork;}- (NSString *)work { return _work;}
���������������������������������������work
������������������������������������
���������������������
������������������������������������������������ categories ���������������������������������������������������������������������������������������������������������������Person
������Person+Country.gch
���������������countryName
���������
���������������
@interface Person (Country)@property (nonatomic, copy) NSString *countryName;@end
������������������������������������������������������������������������������������������������������ setter/getter ������������������������������������������ setter ��� getter ������������������������������������objc_setAssociatedObject
���objc_getAssociatedObject
������������������������
������������������������������������������
@interface Person (Country)@property (nonatomic, copy) NSString *countryName;@end@implementation Person (Country)- (void)setCountryName:(NSString *)str { objc_setAssociatedObject(self, "countryName", str, OBJC_ASSOCIATION_COPY);}- (NSString *)countryName { return objc_getAssociatedObject(self, "countryName");}@end
������VC������������������Person
������������������countryName
��������������������������������� setter ���������
Person *person = [[Person alloc] init];[person setCountryName:@"china"];
������������������������������������������������������������������������������������������������������ setter ���������������������������������
������������������������
������������������������������������������������������������������������������������������������������Student
���������Person
���������������name
��������� setter ������������������������������������������������
Person
������������������������������self.name = @"Default Name";
���������������������Student
���setName:
���������Student
��� setter ������������������������������������������������������name
���������������student������������������������������������������������������������������������������������
- ������:
-[Person setCountryName:]: unrecognized selector sent to instance
- ������: ������������������������������������������������������
setName:
��������������������������������������������������� setter ������������������������������������������ setter ���������������������������������������������
������������������������������������������������������������������ setter ���������������������������������������������������������Student
������������������������
-(id)init { self = [super init]; self.name = @"student"; // ������������������������ return self;}
������������
-(id)init { self = [super init]; [self setName:@"student"]; // ������ setter ������������ return self;}
��������������������������������������������������������������������������������������������������������������������������������������� setter ������������������������������
���������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
