考虑以下两种情况:
// case 1
NSObject *strongOne = [[NSObject alloc] init];
NSObject * __weak weakOne = strongOne;
if (weakOne) {
NSLog(@"weakOne is not nil.");
} else {
NSLog(@"weakOne is nil.");
}
strongOne = nil;
if (weakOne) {
NSLog(@"weakOne is not nil.");
} else {
NSLog(@"weakOne is nil.");
}
输出这个:
weakOne is not nil.
weakOne is not nil.
和
// case 2
NSObject *strongOne = [[NSObject alloc] init];
NSObject * __weak weakOne = strongOne;
strongOne = nil;
if (weakOne) {
NSLog(@"weakOne is not nil.");
} else {
NSLog(@"weakOne is nil.");
}
输出这个:
weakOne is nil.
据我所知,当strongOne
被释放时,对同一对象的弱引用应该更新为nil
.
我的问题:为什么这只发生在case 2
?