1. nsdictionary and nsmutabledictionary
Nsdictionary dictionarywithobjectsandkeys :~, Nil
Create a dictionary using a key-Value Pair and end with the NIL flag
Nsdictionary initwithobjectsandkeys:
Use Key-value pairs to initialize the dictionary, and use nil to indicate the end.
Dictionary count calculates the length of its dictionary.
Dictionary keyeunmerator stores all the keys in nseunmerator, And you can quickly enumerate the values of the keys.
Dictionary objectforkey: The key is used to query the value.
Demo:
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code nsdictionary * dictionary = [nsdictionary dictionarywithobjectsandkeys: @ "I Am a value", @ "I am key1", @ "I Am a value 2 ", @ "I am key2", nil]; // obtain the number of dictionaries int COUNT = [dictionary count]; nslog (@ "Number of dictionaries: % d", count ); // obtain all the key values in the dictionary. nsenumerator * enumeratorkey = [dictionary keyenumerat Or]; // quickly enumerate all key values for (nsobject * object in enumeratorkey) {nslog (@ "traversal key value: % @", object );} // obtain all value values in the dictionary nsenumerator * enumeratorvalue = [dictionary objectenumerator]; // quickly enumerate all values for (nsobject * object in enumeratorvalue) {nslog (@ "traversal value: % @", object);} // locate value nsobject * object = [dictionary objectforkey: @ "I am key2"] through key; if (object! = Nil) {nslog (@ "the value found through key is: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil ); [pool release]; return retval ;}
2. nsmutabledictionary is a subclass of nsdictionary, so it also inherits its methods.
[Nsmutabledictionary dictionarywithcapacity: 10];
// Create a dictionary with a length of 10. However, if the dictionary content exceeds 10, it will automatically increase.
[Nsmutabledictionary initwithcapacity: 10]; // The initialization length is 10; [dictionary setobject :~ Forkey; ~]; // X add data to a variable dictionary; [dictionary removeallobjects]; // delete all data; removeobjectforkey: // Delete the corresponding value of the key;
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code // create a dictionary object, the initialization length is 10 nsmutabledictionary * dictionary = [nsmutabledictionary dictionarywithcapacity: 10]; // dynamically add data to the dictionary [dictionary setobject: @ "added value1" forkey: @ "key1"]; [dictionary setobject: @ "added value2" forkey: @ "key2"]; // locate value nsobject * objec through key T = [dictionary objectforkey: @ "key2"]; If (object! = Nil) {nslog (@ "the value found through key is: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil ); [pool release]; return retval ;}
[Nsmutabledictionary dictionarywithcapacity: 10];
// Create a dictionary with a length of 10. However, if the dictionary content exceeds 10, it will automatically increase.