Create an immutable Dictionary object
// Facilitation Builder Creation
nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:@ "Zhonger", @ "name2", @ "Taixu", @ "name1", @ "Caicai", @ "Name3", Nil] ;
// Initialize Method
//Give value to key first
nsdictionary *dic1 = [[nsdictionary alloc]initwithobjectsandkeys:@ "Zhonger" @ " name2", @ " taixu", @ "name1", @ "Caicai", @ "Name3", Nil ];
nsdictionary *dic2 = [[nsdictionary alloc] initwithobjects:@[@ "name1" , @ "name2", @ "Name3"] forkeys:@[@ "Zhonger", @ "name2", @ "Taixu"];
// laugh grammar
//key in front of value in the rear
nsdictionary *dic3 = @{@ "name1":@ "Zhanger" @ " name2":@ "Caicai" @ "Name3":@ "Fufu"};
// Get the number of key-value pairs in the dictionary, and get all the keys and all the value;
Nsuinteger count = [dic3 count];
nsarray *arr = [dic3 allKeys];
NSLog(@ "dic3 AllKeys is%@", arr);
NSLog(@ "dic3 Allvalue is%@", [dic3 allvalues]);
// Gets a value in the dictionary , via key
nsstring *string = [dic3 objectforkey:@ "name2"];
nsarray *allkeys = [dic3 allkeys];
for (int i = 0; i < [AllKeys count]; i + +) {
NSLog(@ "%@", [dic3 objectforkey: allkeys[i]]);
}
// Create variable Dictionary object, initialize method, convenience constructor, literal
nsmutabledictionary *mdic = [[nsmutabledictionary alloc] initwithcapacity:0 ];
nsmutabledictionary *mdic1 = [nsmutabledictionary dictionarywithcapacity:0 ];
nsmutabledictionary *mdic2 = [@{@ "Key1":@ "Zhangsan" @ "Key2":@ "Lisi" @ "Key3":@ "Wangwu"}mutablecopy];
NSLog(@ "MDic2 is%@", MDic2);
// increase the key-value pair method Setobject:forkey:
[MDic2 setobject:@ "Liumazi" forkey:@ "Key4"];
NSLog(@ "MDic2 is%@", MDic2);
// Delete the key and value pairs, and after deletion , both key and value do not exist.
//removeobjectforkey:
[MDic2 Removeobjectforkey:@ "Key2"];
NSLog(@ "MDic2 is%@", MDic2);
// Remove all key-value pairs
[MDic2 removeallobjects];
NSLog(@ "MDic2 is%@", MDic2);
The method of modifying key-value pairs is the same as adding key-value pairs.
[MDic2 setobject:@ "Zhangsan" forkey:@ "Key1"];
NSLog(@ "MDic2 is%@", MDic2);
[MDic2 setobject:@ "Lisi" forkey:@ "Key1"];
NSLog(@ "MDic2 is%@", MDic2);
//The Key in the dictionary cannot be duplicated , but value can be repeated, and the dictionary is an unordered collection
Objective-c nsdictionary immutable dictionaries and nsmutabledictionary mutable dictionaries