相关疑难解决方法(0)

在强者退出之后,为什么我的弱参考不被清除?

我有点顽固,但我想了解弱弱和强烈的参考,所以这就是为什么我再次问你.

考虑一下:

__weak NSString* mySecondPointer = myText;   
NSLog(@"myText: %@", myText);
Run Code Online (Sandbox Code Playgroud)

结果是myText: (null)非常明显的 - 弱引用在赋值后设置为null,因为没有对尖头对象的强引用.

但在这种情况下:

__strong NSString* strongPtr = [[NSString alloc] initWithFormat:@"mYTeSTteXt %d"]; 
// weak pointer points to the same object as strongPtr
__weak NSString* weakPtr = strongPtr;
if(strongPtr == weakPtr) 
     NSLog(@"They are pointing to the same obj");        
NSLog(@"StrongPtr: %@", strongPtr);
NSLog(@"weakPtr: %@", weakPtr);

NSLog(@"Setting myText to different obj or nil");

// after line below, there is no strong referecene to the created object:
strongPtr = [[NSString alloc] …
Run Code Online (Sandbox Code Playgroud)

weak-references reference-counting objective-c strong-references

28
推荐指数
1
解决办法
3065
查看次数