Immutable dictionary Nsdictionary
Dictionaries have no order, key-value pairs
Key-value
Key (String object)-Value (object)
0. The values of multiple key-value pairs can be the same in a dictionary, but the keys cannot be the same and must be unique
1. Advantages of dictionary Storage
A. Easy to find
B. Easy to hack
2. How to create a dictionary
Nsdictionary *dic = @{@ "Xiao Wang": xw,@ "Zhang San": zs,@ "ls": ls};
Nsdictionary *dic1 = [Nsdictionary dictionary];
Nsdictionary *dic1_ = [[Nsdictionary alloc]init];
Nsdictionary *dic2 = [Nsdictionary dictionarywithdictionary:dic];
Nsdictionary *dic2_ = [[Nsdictionary alloc]initwithdictionary:dic];
Initializes a key-value pair
Nsdictionary *dic3 = [nsdictionary dictionarywithobject:xw forkey:@ "Xiao Wang"];
Create a dictionary with multiple key-value pairs
Nsdictionary *dic4 = [nsdictionary Dictionarywithobjects:@[xw,zs,ls] forkeys:@[@ "Xiaowang", @ "Zhangsan", @ "Lisi"];
3. Get the number of dictionary key-value pairs
NSLog (@ "Count:%ld", Dic5.count);
4, get all keys keys (String object)
Nsarray*allkeys = [Dic5 AllKeys];
5, get all the value
Nsarray *allvalues = [Dic5 allvalue];
6. Traverse a dictionary
For (NSString *key in AllKeys) {
NSLog (@ "key =%@:value =%@", key,[dic5 Objectforkey:key]);
}
7, get the corresponding value by key
Nsarray *temparray = [dic5 objectforkey:@ "Man"]);
Nsmubtabledictionary
1. Creating a mutable Dictionary
Nsmutabledictionary *mdic = [Nsmutabledictionary dictionarywithcapacity:0];
2. Add a key-value pair
[MDic setobject:@ "YQL" forkey:@ "name"];
[MDic setobject:parray forkey:@ "man"];
[MDic setobject:@ "Red Mansions" forkey:@ "classics"];
NSLog (@ "7.%@", mDic);
3. Change the value of a key
[MDic setobject:@ "Hongloumeng" forkey:@ "classics"];
4. Delete key-value pairs
[MDic removeobjectsforkeys:@ "Classics"];
[MDic removeobjectsforkeys:@[@ "man", @ "NB", @ "Hel"];
[MDic removeallobjects];
NSLog (@ "8..%@", mDic);
1224 Dictionary Dictionary