Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting nested NSDictionary and NSArray values

I need to be able to be able to set the values of nested NSDictionary and NSArray. For example:

dictionary array dictionary key1 value1

What is the easiest way to get to key1 and set value1? setValueForKeyPath obviously doesn't work with and array.

like image 749
Devroot Avatar asked Jul 27 '26 17:07

Devroot


1 Answers

Using Objective-C's Object Subscripting.

Imagine the following nested dictionaries and arrays:

dictionaryA => arrayB => dictionaryC
  • arrayB is stored under @"arrayKey" in dictionaryA, and dictionaryC is stored at index dictionary_index into arrayB.
  • dictionaryC should be set to value1 for @"key1";

To set key1, you need to do:

(NSDictionary*)((NSArray*)(dictionaryA[@"arrayKey"])[dictionary_index])[@"key1"] = value1;


See the section Object Subscripting in Clang 3.3 documentation: OBJECTIVE-C LITERALS for more informations.

like image 184
Jean Avatar answered Jul 29 '26 07:07

Jean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!