// Nsdictionary is a data set composed of key-value pairs. Key) the value must be unique ************* * *** // dictionary initialization nsdictionary * dic1 = [nsdictionary dictionarywithobject: @ "value" forkey: @ "key"]; // output: {key = value} nsdictionary * dic2 = [nsdictionary dictionarywithobjects: @ "V1", @ "K1 ", @ "V2", @ "K2", @ "V3", @ "K3", nil]; nsdictionary * dic3 = [nsdictionary dictionarywithdictionary: dic2]; // obtain the number of dictionaries int COUNT = [dic2 count]; // output: Count = 3 (with three key-value pairs) // obtain the nsstring * string = [dic2 objectforkey: @ "K2"] According to the key name; // output: string = v2 // obtain all keys of the dictionary and valuensarray * keyarray = [dic2 allkeys]; // output: keyarray = {K1, K2, k3} nsarray * valuearray = [dic2 allvalues]; // output: valuearray = {V1, V2, v3}/***************** variable dictionary ******************* ** // create a dictionary nsmutabledictionary * mutabledic = [[nsmutabledictionary alloc] initwithobjectsandkeys: @ "V1", @ "K1", @ "V2", @ "K2", @ "V3", @ "K3", nil]; // Add a key-Value Pair // method 1 nsdictionary * dic4 = [nsdictionary dictionarywithobject: @ "V4" forkey: @ "K4"]; [mutabledic addentriesfromdictionary: dic4]; // method 2 [mutabledic setvalue: @ "object" forkey: @ "K5"]; // create an empty variable dictionary nsmutabledictionary * mutabledic2 = [nsmutabledictionary dictionary]; [mutabledic2 setdictionary: mutabledic]; // optional * mutabledic2 = [nsmutabledictionary dictionarywithobject: @ "one" forkey: @ "K"]; // Delete the element based on the key name [mutabledic removeobjectforkey: @ "K3"]; // delete a key-Value Pair nsarray * keys = [nsarray arraywithobjects: @ "K1", @ "K2", @ "K3", nil]; [mutabledic removeobjectsforkeys: Keys]; // delete all elements [mutabledic removeallobjects]; ********************* * *** // generally traverse for (INT Index = 0; index <[mutabledic count]; index ++) {nsstring * object = [mutabledic objectforkey: [mutabledic allkeys] objectatindex: Index]; nslog (@ "% @", object) ;}// quick enumeration for (ID key in mutabledic) {nsstring * object = [mutabledic objectforkey: Key]; nslog (@ "% @", object );} // enumerate nsenumerator * enumerator = [mutabledic keyenumerator]; ID key = [enumerator nextobject]; while (key) {ID object = [mutabledic objectforkey: Key]; nslog (@ "% @", object); Key = [enumerator nectobject];}