我有一个NSDictionary对象,由其键和对象由NSMutableStrings填充。我已经能够通过更改原来的NSMutableString与setString:方法更改键。
I have an NSDictionary object that is populated by NSMutableStrings for its keys and objects. I have been able to change the key by changing the original NSMutableString with the setString: method. They key however remains the same regardless of the contents of the string used to set the key initially.
我的问题是,是保护不被更改的键,意味着它将始终除非我删除它并将另一个添加到字典中,否则是相同的。
My question is, is the key protected from being changed meaning it will always be the same unless I remove it and add another to the dictionary?
谢谢。
推荐答案设置项目时,键是 -copy ',因此您以后无法更改它们。
The keys are -copy'd when the items are set, so you can't changing it afterwards is useless.
向字典添加条目的方法 - 无论是作为初始化(对于所有字典)还是在修改期间(对于可变字典) - 复制每个键参数(键必须符合 NSCopying 协议),并将副本添加到字典。
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,或者只是替换项:
You could use CFDictionary with kCFTypeDictionaryKeyCallBacks, or just replace the item:
id value = [dictionary objectWithKey:oldKey]; [dictionary setObject:value withKey:newKey]; [dictionary removeObjectForKey:oldKey];更多推荐
我可以更改NSDictionaries键吗?
发布评论