Appending NSDictionary to other NSDictionary

2020-05-14 13:40发布

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?

3条回答
来,给爷笑一个
2楼-- · 2020-05-14 13:49

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.

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.

查看更多
我想做一个坏孩纸
3楼-- · 2020-05-14 13:52

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.

查看更多
够拽才男人
4楼-- · 2020-05-14 13:57

You looking for this guy:

[NSMutableDictionary addEntriesFromDictionary:]

Make sure your UITableView dictionary is an NSMutableDictionary!

Check it here

查看更多
登录 后发表回答