- Create a dictionary
- Nsdictionary *dic1 = [nsdictionary dictionarywithobject:@ "value" forkey:@ "key"];
- NSLog (@ "Dic1:%@", Dic1);
- For immutable nsdictionary *dic1 =@{@ "value": @ "key", @ "value1": @ "Key1"};
- Create multiple dictionaries
- Nsdictionary *dic2 = [Nsdictionary Dictionarywithobjectsandkeys:
- @ "value1", @ "Key1",
- @ "value2", @ "Key2",
- @ "Value3", @ "Key3",
- @ "Value4", @ "Key4",
- NIL];
- NSLog (@ "Dic2:%@", Dic2);
- To create a dictionary from an existing dictionary
- Nsdictionary *dic3 = [Nsdictionary dictionarywithdictionary:dic2];
- NSLog (@ "DIC3:%@", DIC3);
- Get value based on key
- NSLog (@ "Key3 value:%@", [dic3 objectforkey:@ "Key3"]);
- Get Dictionary number
- NSLog (@ "dic count:%d", dic3.count);
- All the key sets
- Nsarray *keys = [dic3 AllKeys];
- NSLog (@ "Keys:%@", keys);
- All Values Collection
- Nsarray *values = [dic3 allvalues];
- NSLog (@ "values:%@", values);
- Nsmutabledictionary *mutabledic = [[Nsmutabledictionary alloc] Initwithobjectsandkeys:
- @ "mvalue1", @ "Mkey1",
- @ "Mvalue2", @ "Mkey2", nil];
- To add an existing dictionary data
- [Mutabledic ADDENTRIESFROMDICTIONARY:DIC3];
- NSLog (@ "Mutabledic:%@", mutabledic);
- Adding a new key-value object
- [Mutabledic setvalue:@ "Set1" forkey:@ "SetKey1"];
- NSLog (@ "Set value for key:%@", mutabledic);
- Overwrite old dictionary data with new dictionary data
- [Mutabledic Setdictionary:dic2];
- NSLog (@ "Set dictionary:%@", mutabledic);
- Delete value based on key
- [Mutabledic removeobjectforkey:@ "Key1"];
- NSLog (@ "Removeforkey:%@", mutabledic);
- Fast traversal
- For (ID key in mutabledic) {
- NSLog (@ "key:%@ value:%@", Key, [Mutabledic Objectforkey:key]);
- }
- Enumeration traversal
- Nsenumerator *enumerator = [Mutabledic keyenumerator];
- ID key = [enumerator nextobject];
- while (key) {
- NSLog (@ "Enumerator:%@", [Mutabledic Objectforkey:key]);
- key = [Enumerator nextobject];
- }
- Delete an element based on the key array
- [Mutabledic Removeobjectsforkeys:keys];
- NSLog (@ "Removeobjectsforkeys:%@", mutabledic);
- [Mutabledic removeallobjects];
- Delete all elements
- NSLog (@ "Remove all:%@", mutabledic);
- }
- return 0;
- }
IOS Nsdictionary Dictionary