Through the learning and understanding of Array and Set, we can imagine that Dictionary can be divided into two types: variable and immutable. Indeed, two dictionary classes are provided in Object C: NSDictionary and NSMutableDictionary. in. NET, we have also learned the Dictionary class. This set is stored in key-value pairs. In addition, it is extremely simple to use Dictionary to search for elements. An NSDictionary unchangeable Dictionary (1) initialize the dictionary copy code NSDictionary * dic = [NSDictionary dictionaryWithObject: @ "hechen" forKey: @ "name"]; NSDictionary * dic1 = [NSDictionary dictionary Syntax: @ "hechen", @ "name", @ "25", @ "age", nil]; NSDictionary * dic2 = [[NSDictionary alloc] init]; NSLog (@ "% d", [dic count]); NSLog (@ "% d", [dic1 count]); NSLog (@ "% d ", [dic2 count]); the copy code lists three methods to initialize dictionary objects: 1. [[NSDictionary alloc] init] Method It is most common to allocate space for NSDictionary and reinitialize it. 2. use dictionaryWithObject forkey. This method provides two parameters during initialization. The first parameter is the stored value and the second parameter is the key.. NET. The key here is placed behind it. 3. The method dictionaryWithObjectsAndKeys is also used to initialize NSDictionary. Multiple values can be input here. The odd value is the value, and the even value is the key value, but must end with the nil format. This is consistent with the previous NSArray and NSSet to identify the end of the set. (2) obtain the length of NSDictionary. The length of NSDictionary is obtained using the count method. This is the same as that of most collections. In the test example above, three NSDictionary lengths are obtained, the output results are 1, 2, and 0 respectively. The differences between the above three methods are also illustrated. (3) obtain the NSEnumerator * enumer = [dic1 keyEnumerator]; NSString * key2 = [enumer nextObject]; while (key2! = Nil) {NSLog (@ "output Key = % @", key2); key2 = [enumer nextObject];} the above code can be seen that the keyEnumerator method is used to obtain the collection iteration of the NSDictionary Key, and then the iterator is used to traverse and output all elements. (4) the method for obtaining the Value in the Values iteration test case in NSDictionary is similar to that for obtaining the key. Here, the result of loop traversal is somewhat different, but the two results are the same, note: (5) obtain Value NSString * va = [dic1 objectForKey: @ "name"]; NSLog (@ "objectForKey = % @", va) based on the Key ); in Object C, the method objectForKey is used to obtain the Value through the key. In the above case, the usage is simple and clear. 2. NSMutableDictionary is a subclass of NSDictionary and inherits all its methods and attributes. However, the difference with NSDictionary is that NSMutableDictionary can dynamically modify and add Delete elements. (1) set the initial capacity of NSMutableDictionary * muDic1 = [NSMutableDictionary dictionaryWithCapacity: 10] by using the dispatch method. The code above initializes an NSMutableDictionary object and sets its initial capacity to 10; like NSMutableArray and NSMutableSet, when the storage Element reaches the maximum capacity, it will automatically expand the capacity, so you don't have to worry about array out-of-bounds. (2) Add an element to NSMutableDictionary to copy the Code [muDic1 setObject: @ "China" forKey: @ "name"]; [muDic1 setObject: @ "area" forKey: @ "add"]; NSEnumerator * enumerKeys = [muDic1 keyEnumerator]; for (NSString * key in enumerKeys) {NSString * value = [muDic1 objectForKey: key]; NSLog (@ "output value = % @", value);} copy the code. From the code above, you can see that the setObject forKey method can be used to add elements to the dictionary, the first parameter of the method is value, and the last parameter is key. Use a loop to get the corresponding value. (3) Delete the [muDic1 removeObjectForKey: @ "add"]; for (NSString * key in [muDic1 keyEnumerator]) {NSLog (@ "----- % @", [muDic1 objectForKey: key]);} the code above deletes the elements in NSMutableDictionary based on a key value. The method used is removeObjectForKey, in NSMutableDictionary, you can also delete elements based on multiple key values. -(Void) removeObjectsForKeys :( NSArray *) keyArray; this method parameter is an array used to delete all elements of the key contained in the array. Copy the Code [muDic1 removeObjectsForKeys: [NSArray arrayWithObjects: @ "AB", nil]; [muDic1 removeAllObjects]; for (NSString * key in [muDic1 keyEnumerator]) {NSLog (@ "********** % @", [muDic1 objectForKey: key]);} copy the first code above to delete the contained elements from the array, and the second is to delete all elements in NSMutableDictionary. There is no final result output. The above is a brief introduction to dictionary-related content. The methods provided by the system are far more than that, and we still need to work hard and Practice continuously. practice is always the most effective method.