gpt4 book ai didi

ios - NSHashTable weakObjectsHashTable – 添加的对象未归零

转载 作者:行者123 更新时间:2023-12-03 18:21:33 26 4
gpt4 key购买 nike

我正在尝试了解 ARC 和 NSHashTable weakObjectsHashTable 的工作原理。我添加到哈希表中的对象应该被删除/清零,或者一旦对象被释放,它们就会变成什么。下面在其 NSLog 中的代码示例显示该对象仍然存在于哈希表中。我做错了什么?

#import <Foundation/Foundation.h>

int main(int argc, char *argv[])
{
@autoreleasepool
{
NSHashTable *hashTable = [NSHashTable weakObjectsHashTable];

@autoreleasepool
{
NSString *str = @"Hello World!";
[hashTable addObject:str];
str = nil;
}

NSLog(@"hashTable:%@", [hashTable allObjects]);
// prints: hashTable:("Hello World!") – but should be empty?
}
}

最佳答案

分配 nil 对象不会影响结果。 NSString *str = @"Hello World!";对象引用不弱! Apple 的文档说:如果在这样的哈希表中没有对对象的强引用,那么这些对象将被释放。

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Collections/Articles/Sets.html#//apple_ref/doc/uid/20000136-CJBDHAJD

关于ios - NSHashTable weakObjectsHashTable – 添加的对象未归零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006410/

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