gpt4 book ai didi

objective-c - Objective-C 中的弱引用何时更新为 nil?

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:08 24 4
gpt4 key购买 nike

<分区>

考虑以下两种情况:

// 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

我的问题:为什么这只发生在案例 2 中?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com