Two classes are provided in objective-c to implement dictionaries, namely nsdictionary and nsmutabledictionary. Among them, Nsmutabledictionary is a subclass of Nsdictionary, which inherits the parent class method while supporting the insertion and deletion of data, which together implements the storage of data in the form of " key-value pairs ".
// The dictionary implemented in Objective-c has one of the biggest differences compared to other languages: the same dictionary can hold different types of objects.
1 NSDictionary1.1 Creating immutable dictionaries
You can use the class method Dictionarywithobjectsandkeys: To create and initialize a dictionary, as in the following example:
Nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys: @ "val1" @" Key1 " , @" Val2 " @" Key2 ", nil];
Note: When initializing a dictionary with a key-value pair, the "value" is in front, the "key" is followed by a pair, each parameter is separated by commas, and the end is ended with nil . The dictionary memory distribution created using the above method is as follows:
1.2 Get dictionary size
Before performing a dictionary operation, it is often necessary to get the dictionary size, that is, to determine the number of "key-value pairs" in the dictionary, we can use the count message, using the following method:
int cnt = [dict count];
The method returns a variable of type Nsinteger, and we can also receive it directly using a variable of type int.
1.3 Get all the keys in the dictionary
Sending a keyenumerator message to a dictionary returns an enumeration object that stores all the keys in the dictionary:
Nsenumerator *enumkeys = [Dict keyenumerator]; for inch Enumkeys) { NSLog (@ "enumkey:%@", obj);}
You can use the "for-in" syntax to implement the traversal of an enumerable object, and the loops in the above code can traverse all the keys and output:
--£ º11.873 octest[2547:94272 ] enumkey:key1-ten:13.753 octest[2547:94272] Enumkey:key2
Note: If we traverse the Dictionary object directly using the "for-in" syntax, it is actually the equivalent of a "key" traversal.
for inch dict) { NSLog (@ "key in dic:%@", obj);}
The output of executing the above code is:
£ º17.590 octest[2667:97958 in dic:key1-Ten: 17.590 octest[2667:97958 in Dic:key2
1.4 Traversing all the value in a dictionary
You can get the "value" in the dictionary using a method similar to get "key", just send the objectenumerator message to the dictionary:
Nsenumerator *enumvalues = [Dict objectenumerator]; for inch enumvalues) { NSLog (@ "value in dic:%@", obj);}
The results of the operation are as follows:
-£º41.984 octest[2702:100424 in dic:val1--: 41.984 octest[2702:100424 in Dic:val2
1.5 Retrieving value by key
For the dictionary, the most common operation is to find, that is, according to the given key, retrieve the corresponding value, the method is as follows:
NSObject *value = [dict objectforkey:@ "key1"]; if (value = nil) { NSLog (@ "value in dict:%@", Value);}
The results of the operation are as follows:
--£ º09.012 octest[2736 :103393 in Dict:val1
Summary of how dictionaries are used in objective-c