更改 NSDictionary 中的键名称
我有一个方法,它返回一个带有某些键和值的 nsdictionary。我需要将字典中的键名称更改为新的键名称,但该键的值需要相同,但我被困在这里。需要帮助
I have a method which returns me a nsdictionary with certain keys and values. i need to change the key names from the dictionary to a new key name but the values need to be same for that key,but i am stuck here.need help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此方法仅适用于可变字典。如果新密钥已经存在,它不会检查应该做什么。
您可以通过调用 mutableCopy 来获取不可变对象的可变字典。
This method will only work with a mutable dictionary. It doesn't check what should be done if the new key already exists.
You can get a mutable dictionary of a immutable by calling mutableCopy on it.
为了将特定键更改为新键,我为类别类编写了一个递归方法。
To change specific key to new key, I have written a recursive method for Category Class.
您无法更改 NSDictionary 中的任何内容,因为它是只读的。
循环遍历字典并使用新的键名创建一个新的 NSMutableDictionary 怎么样?
You can't change anything in an NSDictionary, since it is read only.
How about loop through the dictionary and create a new NSMutableDictionary with the new key names ?
您不能使用旧值添加新的键值对,然后删除旧的键值对吗?
这只适用于
NSMutableDictionary
。NSDictionarys
的设计初衷是一旦创建就不能更改。Could you not add a new key-value pair using the old value, and then remove the old key-value pair?
This would only work on an
NSMutableDictionary
.NSDictionarys
are not designed to be changed once they have been created.