I. Basic concepts of dictionaries The dictionary (NSDictionary, NSMutableDictionary) in Foundation is a data set composed of key-value pairs, just as we define words in the dictionary. Key (key) is used to find the corresponding value. key is usually a String object or any other type of object. in a dictionary object, the key value must be unique. In addition, the key and value of the dictionary object cannot be null (nil). If you need to add a null value to the dictionary, you can add the NSNull object. Ii. Immutable dictionary-NSDictionary 1: Initialize (with one element and multiple elements ): // Initialize with a key-Value Object NSDictionary * dict1 = [NSDictionary dictionaryWithObject: numObj1 forKey: @ "key1"]; NSLog (@ "% @", dict1 ); // initialize NSDictionary * dict2 = [NSDictionary dictionaryWithObjectsAndKeys: numObj1, @ "key1", numObj2, @ "key2", nil] with multiple elements; NSLog (@ "% @", dict2 ); 2: Get the number of dictionaries // Obtain the number of dictionaries NSInteger count = [dict2 count]; NSLog (@ "% ld", count ); 3: Get the corresponding value object based on the key // Obtain the corresponding value object based on the key NSString *value=[dict2 objectForKey:@"key1"]; NSLog(@"%@",value); 4: Convert the dictionary key into an enumeration object for Traversing NSEnumerator * enumerator = [dict2 keyEnumerator]; 5. obtain all the key sets. // Obtain all the key Sets NSArray *keys= [dict2 allKeys]; NSLog(@"%@",keys); 6. Obtain all value sets. // Obtain all value Sets NSArray *values=[dict2 allValues]; NSLog(@"%@",values); Iii. Variable dictionary NSMutableDictionary inherited from NSDictionary 1: Initialize the dictionary // Initialize the dictionary NSMutableDictionary * mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @ "v1", @ "k1", @ "v2", @ "k2", @ "v3 ", @ "k3", @ "v4", @ "k4", @ "v5", @ "k5", nil]; 2: Add a specified dictionary to the dictionary // Add a dictionary to a dictionary NSDictionary *dict4=[NSDictionary dictionaryWithObject:@"v6" forKey:@"k6"]; [mutableDic addEntriesFromDictionary:dict4]; NSLog(@"%@",mutableDic); 3: add value and key to the dictionary // Add value and key to the dictionary [mutableDic setValue: @ "object" forKey: @ "key"]; 4: Create an empty dictionary and set a new dictionary.
// Create an empty dictionary, and set a new dictionary to overwrite all previous dictionaries. * mutableDict2 = [NSMutableDictionary dictionary]; [mutableDict2 setDictionary: mutableDic]; NSLog (@ "% @", mutableDict2 ); 5. Delete the value of the specified key. // Remove the value of the specified key [mutableDict2 removeObjectForKey: @ "k4"]; NSLog (@ "% @", mutableDict2 ); 6. Delete the value from the key set. // Delete the value NSArray * arrayKeys = [NSArray arrayWithObjects: @ "k1", @ "k2", @ "k3", nil]; [mutableDict2 removeObjectsForKeys: arrayKeys]; NSLog (@ "% @", mutableDict2 ); 7. Delete all values in the dictionary. // Delete all data in the dictionary [mutableDict2 removeAllObjects]; NSLog(@"%@",mutableDict2); Iv. Enumeration Traversal 1: General Traversal // General Traversal NSArray *allKeys=[mutableDic allKeys]; for (NSInteger i=0; i<[allKeys count]; i++) { NSString *obj=[mutableDic objectForKey:[allKeys objectAtIndex:i]]; NSLog(@"%@",obj); } 2: Quick Enumeration // Fast Traversal for (id key in mutableDic) { NSString *object=[mutableDic objectForKey:key]; NSLog(@"%@",object); } 3: iterator Enumeration // NSEnumerator * enumerator1 = [mutableDic keyEnumerator]; id key = [enumerator1 nextObject]; while (key) {id object = [mutableDic objectForKey: key]; NSLog (@ "% @", object); key = [enumerator1 nextObject];} |