gpt4 book ai didi

objective-c - 我可以更改 NSDictionary 的键吗?

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

我有一个 NSDictionary 对象,它由 NSMutableString 的实例填充,用于其键和对象。我已经能够通过使用 setString: 方法更改原始 NSMutableString 来更改 key 。然而,无论最初用于设置 key 的字符串内容如何,​​它们的 key 都保持不变。

我的问题是, key 是否受到保护不被更改,这意味着除非我删除它并向字典中添加另一个,否则它将始终相同?

谢谢。

最佳答案

键是复制的,当项目被设置时,所以你不能改变它之后是没有用的。

Methods that add entries to dictionaries—whether as part of initialization (for all dictionaries) or during modification (for mutable dictionaries)—copy each key argument (keys must conform to the NSCopying protocol) and add the copies to the dictionary. Each corresponding value object receives a retain message to ensure that it won’t be deallocated before the dictionary is through with it.

您可以将 CFDictionary 与 kCFTypeDictionaryKeyCallBacks 一起使用,或者只是替换该项目:

id value = [dictionary objectWithKey:oldKey];
[dictionary setObject:value withKey:newKey];
[dictionary removeObjectForKey:oldKey];

关于objective-c - 我可以更改 NSDictionary 的键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2797321/

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