ios 将 NSDictionary 附加到其他 NSDictionary
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9254400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending NSDictionary to other NSDictionary
提问by slonkar
I have one NSDictionary
and it loads up UITableView
. If a user scrolls more and more, I call API and pull new data. This data is again in the form of an NSDictionary
. Is it possible to add the new NSDictionary
to the existing one?
我有一个NSDictionary
,它加载了UITableView
。如果用户滚动得越来越多,我会调用 API 并提取新数据。此数据再次采用NSDictionary
. 是否可以将新添加NSDictionary
到现有的?
回答by MGA
You looking for this guy:
你在找这个人:
[NSMutableDictionary addEntriesFromDictionary:]
Make sure your UITableView
dictionary is an NSMutableDictionary
!
确保您的UITableView
字典是NSMutableDictionary
!
回答by Hot Licks
Use NSMutableDictionary addEntriesFromDictionary to add the two dictionaries to a new mutable dictionary. You can then create an NSDictionary from the mutable one, but it's not usually necessary to have a dictionary non-mutable.
使用 NSMutableDictionary addEntriesFromDictionary 将两个字典添加到一个新的可变字典中。然后,您可以从可变的 NSDictionary 创建一个 NSDictionary,但通常不需要非可变的字典。
回答by Bill Burgess
Is your NSDictionary full of other NSDictionaries? It sounds like you would need an NSMutableArray that you could add NSDictionaries to at the end. Assuming you can control the flow of data coming in and wouldn't run the risk of duplicates in your array, you could certainly append the data.
您的 NSDictionary 是否充满了其他 NSDictionary?听起来你需要一个 NSMutableArray,你可以在最后添加 NSDictionaries。假设您可以控制传入的数据流并且不会冒数组中重复的风险,您当然可以附加数据。
NSMutableArray *array = [[NSMutableArray alloc] init];
arrayCount = [array count]; // check the item count
[array addObject:dictToAppend];
Without seeing how you are implementing it, I can't provide more detailed code examples. Appending items is easy to do, but know it can only be done with mutable versions of Arrays or Dictionaries. Hope this helps a little.
没有看到您是如何实现它的,我无法提供更详细的代码示例。附加项目很容易做到,但要知道它只能通过数组或字典的可变版本来完成。希望这有所帮助。