<分区>
Possible Duplicate:
Why do weak NSString properties not get released in iOS?
我是 Objective C 的新手,我有一些我自己无法回答的问题。 我有一段代码用于测试 __weak 变量(当然我使用的是 ARC):
NSString *myString = [[NSString alloc] initWithFormat:@"John"];
NSString * __weak weakString = myString;
myString = nil; //<-- release the NSString object
NSLog(@"string: %@", weakString);
上面代码的输出符合预期,因为 weakString 是一个弱变量:
2013-01-02 11:42:27.481 ConsoleApp[836:303] string: (null)
但是当我将代码修改为:
NSString *myString = [[NSString alloc] initWithFormat:@"John"];
NSString * __weak weakString = myString;
NSLog(@"Before: %@", weakString); //<--- output to see if the __weak variable really works.
myString = nil;
NSLog(@"After: %@", weakString);
输出完全不是我所期望的:
2013-01-02 11:46:03.790 ConsoleApp[863:303] Before: John
2013-01-02 11:46:03.792 ConsoleApp[863:303] After: John
后者的 NSLog 的输出一定是 (nil) 而不是“John”。我尝试在许多文档中进行搜索,但没有找到此案例的答案。 有人可以给出合理的解释吗?提前致谢。