我可以将对象插入 NSDictionary 吗?
我正在从我们的网络服务器中提取坐标列表。我将整个列表存储为 NSDictionary。当我使用数据(纬度、经度、半径、名称)时,我想用 1 个或 2 个以上字段更新数据列表。有没有办法将这两个项目插入到现有的字典中?
我唯一现有的想法是,我必须将数据加载到 NSMutableDictionary 中以允许添加,然后创建新字典并用新的更新数据覆盖现有字典。希望有一种更简单或更干净的方式,而不是贫民区操纵它。提前致谢。
I am pulling in a list of coordinates from our web server. I'm storing the whole list as an NSDictionary. While I am using the data (lat, long, radius, name) I would like to update the list of data with 1 or 2 more fields. Is there a way to insert these 2 items to the existing dictionary?
My only existing thought is that I would have to load the data into an NSMutableDictionary to allow additions and just create the new dictionary and overwrite the existing one with the new updated data. Hoping for a simpler or cleaner way rather than ghetto rigging it. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 文档:
所以,
NSDictionary
是不可变的——你不能添加它。使用NSMutableDictionary
并不是不干净的或“贫民区操纵”......这就是NSMutableDictionary
的全部目的。From the docs:
So,
NSDictionary
is immutable--you may not add to it. Using anNSMutableDictionary
is not unclean or "ghetto rigging"...this is the whole purpose for anNSMutableDictionary
.您的想法是正确的,您必须创建一个 NSMutableDictionary,因为 NSDictionary 无法修改。
如果 NSDictionary 中有任何不可变数组或字典,则可能需要深层副本进行修改。
Your thought is correct, you have to create an NSMutableDictionary, as the NSDictionary can not be modified.
If you have any immutable arrays or dictionaries in the NSDictionary, you might need a deep copy for modifcations.