Evgeniy Kleban Evgeniy Kleban - 1 year ago 157
iOS Question

Clear arrays inside NSDictionary

I want to implement "expanded" behaviour on click on table view headers. For that, i have NSDictionary, which is have all data in form key -> array of values.

What i want is, create other dictionary, copy of initial, and remove all data in arrays inside it. So, in initial loading, our table will look like "closed" headers, after tap on each one, it will collaps and show values corresponding to given key. After tap on header aggain, it will "close" and hide values.

So, basically i want to:

1) enumerate through an NSDictionary and remove all data from array (or create new empty arrays)
2) dynamically add/remove data for given key

Is there easy way to achieve that?

Answer Source

How about this:

NSMutableDictionary *newDict = [NSMutableDictionary new];
for id aKey in tableDict {
  newDict[aKey] = [NSMutableArray new];
tableDict = newDict;
[tableView reloadData];


To clear a single key

tableDict[specificKey] = [NSMutableArray new];

To copy the array from one key into another:

tableDict[specificKey] = [((NSMutableArray *)tableDict[otherKey]) mutableCopy];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download