在协议、分类、继承中使用属性的研究
发布日期:2021-05-14 19:08:30 浏览次数:18 分类:精选文章

本文共 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 ������������������������������


    ���������������������������������������������������������������������������������������������������������������������������������������

    上一篇:runtime系列--基本数据类型
    下一篇:HealthKit框架简介

    发表评论

    最新留言

    路过按个爪印,很不错,赞一个!
    [***.219.124.196]2025年04月14日 11时18分31秒