所以我有一个名为description的Nsstring属性,定义如下:
@property (strong,nonatomic) NSMutableString *description;
我在定义getter时可以将它称为_description,如下所示:
- (Nsstring *)description { return _description; }
但是,当我定义一个setter时,如下:
-(void)setDescription:(NSMutableString *)description { self.description = description; }
它从前面提到的getter(未声明的标识符)中断了_description.我知道我可能只是使用self.description,但为什么会这样呢?
解决方法
@borrrden的回答非常好.我只是想补充一些细节.
@property (strong,nonatomic) NSMutableString *description;
它是自动合成的.含义:如果你不提供自己的getter setter(参见borrrden的答案),就会创建一个实例变量(默认情况下它的名称为“underscore propertyName”).根据您提供的属性描述(强,非原子)合成getter setter.
因此,当您获取/设置属性时,它实际上等于调用getter或seter.所以
self.description;
等于[自我描述].
和
self.description = myMutableString;
等于[self setDescription:myMutableString];
因此,当你像你一样定义一个setter:
-(void)setDescription:(NSMutableString *)description { self.description = description; }
它会导致无限循环,因为self.description = description;调用[self setDescription:description] ;.
原文地址:https://www.jb51.cc/iOS/335314.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。