Java has a Map, which can store data in the form of key-value pairs. When the value is set, the corresponding value can be directly obtained through the key, which is convenient and quick. In Objective-C language, dictionaries do this. Like NSArray, a dictionary object can also store different types of values, the dictionaries also have immutable dictionaries and variable dictionaries (NSDictionary and NSMutableDictionary). The former is thread-safe, and the latter is not.
1. NSDictionary:
[NSDictionary dictionaryWithObjectsAndKeys: ..]: Create a dictionary object directly using a key-Value Pair and end with the nil sign.
[Dictionary count]: number of key-value pairs in the dictionary.
[Dictionary keyEnumerator]: stores all keys of a dictionary in NSEnumerator, similar to the iterator in Java.
[Dictionary objectEnumerator]: stores all dictionary values in NSEnumerator.
[Dictionary objectForKey: key]: You can get the stored value of the current key by passing in the key object.
Sample Code:
Int main (int argc, const char * argv []) {@ autoreleasepool {NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "25", @ "age", @ "Zhang San ", @ "name", @ "male", @ "gender", nil]; NSLog (@ "% lu", [dictionary count]); NSEnumerator * enumeratorKey = [dictionary keyEnumerator]; for (NSObject * object in enumeratorKey) {NSLog (@ "key: % @", object );} NSEnumerator * enumeratorObject = [dictionary objectEnumerator]; for (NSObject * object in enumeratorObject) {NSLog (@ "value: % @", object );} NSLog (@ "the key name value is: % @", [dictionary objectForKey: @ "name"]) ;}return 0 ;}
Print the result;
16:31:18. 276 objectiveC [2965: 403] 32012-07-09 16:31:18. 282 objectiveC [2965: 403] key: age2012-07-09 16:31:18. 282 objectiveC [2965: 403] key: name2012-07-09 16:31:18. 283 objectiveC [2965: 403] key: Gender 16:31:18. 283 objectiveC [2965: 403] value: 252012-07-09 16:31:18. 284 objectiveC [2965: 403] value: James 16:31:18. 284 objectiveC [2965: 403] value: 16:31:18. 285 objectiveC [2965: 403] the value of key name is: Zhang San
2. Variable dictionary NSMutableDictionary.
NSMutableDictionary is a subclass of NSDictionary, so it inherits the NSDictionary method. The above Code is fully available for NSMutableDictionary. Let's try something different.
Add or delete key-value data.
[Dictionary setObject: forKey:]: dynamically add data to a variable dictionary
[Dictionary removeAllObjects ..]: deletes all data in the dictionary.
[Dictionary removeObjectForKey ..]: deletes the data of the specified key in the dictionary.
Sample Code:
Int main (int argc, const char * argv []) {@ autoreleasepool {NSMutableDictionary * dictionary = [NSMutableDictionary dictionary Syntax: @ "25", @ "age", @ "Zhang San ", @ "name", @ "male", @ "gender", nil]; [dictionary setObject: @ "30" forKey: @ "ranking"]; NSLog (@ "% lu", [dictionary count]); NSEnumerator * enumeratorKey = [dictionary keyEnumerator]; for (NSObject * object in enumeratorKey) {NSLog (@ "key: % @ ", object);} NSEnumerator * enumeratorObject = [dictionary objectEnumerator]; for (NSObject * object in enumeratorObject) {NSLog (@" value: % @ ", object );} NSLog (@ "key ranking value: % @", [dictionary objectForKey: @ "ranking"]); [dictionary removeObjectForKey: @ "term"]; NSLog (@ "% lu", [dictionary count]);} return 0 ;}
Print result:
16:37:07. 037 objectiveC [3053: 403] 42012-07-09 16:37:07. 042 objectiveC [3053: 403] key: age2012-07-09 16:37:07. 043 objectiveC [3053: 403] key: Gender 16:37:07. 043 objectiveC [3053: 403] key: name2012-07-09 16:37:07. 044 objectiveC [3053: 403] key: Ranking 16:37:07. 044 objectiveC [3053: 403] value: 252012-07-09 16:37:07. 045 objectiveC [3053: 403] value: Male 16:37:07. 045 objectiveC [3053: 403] value: Zhang San 16:37:07. 046 objectiveC [3053: 403] value: 30 16:37:07. 046 objectiveC [3053: 403] key ranking value: 30