Nsdictionary
// Initialize 1.
Nsdictionary * dic11 = [[nsdictionary alloc] initwithobjects: @ [@ "qwe", @ "ASD", @ "zxc", @ "QAZ", @ "wsx"] forkeys: @ [@ "111", @ "222", @ "333", @ "444", @ "555"];
// Obtain all the keys and values respectively.
Nslog (@ "% @", dic11.allkeys );
Nslog (@ "% @", dic11.allvalues );
// Obtain the total number of dictionary key-value pairs
Nslog (@ "% d", (dic11.count ));
// Dictionary traversal and quick Enumeration
For (ID key in dic11 ){
ID value = [dic11 objectforkey: Key];
Nslog (@ "% @: % @", key, value );
}
// You can initialize a literal constant like an immutable dictionary. The key is in the front and the value is in the back.
Nsdictionary * [email protected] {@ "Keya": @ "AAA", @ "keyb": @ 111 };
Nslog (@ "% @", dic00 );
// Other initialization Methods
// Initialization of a key-Value Pair dictionary
Nsdictionary * dic1 = [nsdictionary dictionarywithobject: @ "10001" forkey: @ "000"];
// Initialize dictionary with the same content as dic1
Nsdictionary * dic0 = [nsdictionary dictionarywithdictionary: dic1];
// Initialize the dictionary with multiple key-value pairs
Nsdictionary * dic2 = [nsdictionary dictionarywithobjects: @ [@ "11", @ "22", @ "33", @ "44"] forkeys: @ [@ "first ", @ "second", @ "third", @ "four"];
// Initialize the dictionary with multiple key-value pairs 2
Nsdictionary * dic3 = [nsdictionary dictionarywithobjectsandkeys ", @ "Xiang zhiduo", @ "tease the Holy Buddha", nil];
Nslog (@ "% @", dic3 [@ ""]);
// Create a variable dictionary with the same content as dic2
Nsmutabledictionary * dic4 = [nsmutabledictionary dictionarywithdictionary: dic2];
// Insert a key-value pair with the same name to overwrite the original value
[Dic4 setobject: @ "jjj" forkey: @ "Jiji"];
Nslog (@ "% @", dic4 );
// Insert a key-value pair with the same name to overwrite the original value
[Dic4 setobject: @ "999" forkeyedsubscript: @ "Lan"];
[Dic4 setobject: @ "004" forkeyedsubscript: @ "four"];
Nslog (@ "% @", dic4 );
// Splice dic1 to the front of DIC
[Dic4 addentriesfromdictionary: dic1];
Nslog (@ "% @ ------", dic4 );
// Remove a key-Value Pair
[Dic4 removeobjectforkey: @ "third"];
Nslog (@ "% @", dic4 );
// Remove the key-value pair corresponding to the key in the array
[Dic4 removeobjectsforkeys: @ [@ "five", @ "000"];
Nslog (@ "% @", dic4 );
// Replace all the original content of dic4 with the content of dic11.
[Dic4 setdictionary: dic11];
Nslog (@ "% @", dic4 );
// Save the dictionary content to the file in the specified path
[Dic4 writetofile: @ "/users/Apple/desktop/dic4.plist" atomically: Yes];
// Read the file content from the specified path
Nsdictionary * dic5 = [nsdictionary dictionarywithcontentsoffile: @ "/users/Apple/desktop/dic4.plist"];
Nslog (@ "% @", dic5 );
// Dict3 and dict2 have the same content and are essentially a new object.
// Dictionary is an initialization method for retrieving content from a specified path file. The file must exist. Otherwise, the content is empty.
Nsdictionary * dic6 = [[nsdictionary alloc] initwithcontentsoffile: @ "/users/Apple/desktop/dic4.plist"];
Nslog (@ "% @", dic6 );
// Remove all key-value pairs.
[Dic4 removeallobjects];
Nslog (@ "% @", dic4 );