ary*_*axt 34 properties objective-c automatic-ref-counting
以下是我以前编写自定义保留 setter的方法:
- (void)setMyObject:(MyObject *)anObject
{
[_myObject release], _myObject = nil;
_myObject = [anObject retain];
// Other stuff
}
Run Code Online (Sandbox Code Playgroud)
当属性设置为strong时,如何使用ARC实现此目的.如何确保变量具有强指针?
Dan*_*ark 66
在strong
对伊娃水平需要照顾自己,所以你可以仅仅做
- (void)setMyObject:(MyObject *)anObject
{
_myObject = anObject;
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
就是这样.
注意:如果你这样做没有自动属性,那么ivar就是
MyObject *_myObject;
Run Code Online (Sandbox Code Playgroud)
然后ARC会为您处理保留和发布(谢天谢地).__strong
默认情况下是限定符.
只是总结一下答案
.h文件
//If you are doing this without the ivar
@property (nonatomic, strong) MyObject *myObject;
Run Code Online (Sandbox Code Playgroud)
.m文件
@synthesize myObject = _myObject;
- (void)setMyObject:(MyObject *)anObject
{
if (_myObject != anObject)
{
_myObject = nil;
_myObject = anObject;
}
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18770 次 |
最近记录: |