Ric*_*rdo 1 iphone cocoa objective-c ios
我读了很多关于这个主题的帖子,但我无法完全理解一切.好的,很清楚
self.text = @"MyText" will call the accessory method setText (autogenerated)
_text = @"MyText" will still assign the value but will not call the setText
Run Code Online (Sandbox Code Playgroud)
这很清楚.
但是当我们不使用ARC时,这可能很有用,因为setText将负责内存管理.但是当我们使用ARC时会发生什么?有时如果我使用_text一切正常,有时候如果我不使用"self.text"我的应用程序将无法工作.
那么真正的区别是什么?必须有的不仅仅是内存管理.
让我说我有这个
@interface MyClass:NSObject {
NSMutableString *text;
}
@property (nonatomic ) NSMutableString *text;
Run Code Online (Sandbox Code Playgroud)
在这种情况下是不是相同的呼吁
self.text = @"ok"
Run Code Online (Sandbox Code Playgroud)
要么
text = @"ok" ?
Run Code Online (Sandbox Code Playgroud)
有什么不同?
该属性的基础实例变量实际上是_text.这就是自动合成属性的工作原理.
但是,您应该考虑使用访问器来设置属性(使用self.text =代替).有关在目标c中使用ivars vs属性的原因的详细信息,请参阅此链接
| 归档时间: |
|
| 查看次数: |
270 次 |
| 最近记录: |