Advertisements
NSMutableDictionary by example
+ dictionaryWithCapacity:
Creates and returns a mutable dictionary, initially giving it enough allocated memory to hold a given number of entries.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1]; NSLog(@"%@",dict);
Output
2014-03-22 18:16:58.178 iOS-Tutorial[2727:a0b] { }
- initWithCapacity:
Initializes a newly allocated mutable dictionary, allocating enough memory to hold numItems entries.
Example
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:1]; NSLog(@"%@",dict);
Output
2014-03-22 18:16:58.178 iOS-Tutorial[2727:a0b] { }
- init
Initializes a newly allocated mutable dictionary.
Example
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSLog(@"%@",dict);
Output
2014-03-22 18:16:58.178 iOS-Tutorial[2727:a0b] { }
+ dictionaryWithSharedKeySet:
Creates a mutable dictionary which is optimized for dealing with a known set of keys.
Example
id sharedKeySet = [NSDictionary sharedKeySetForKeys:@[@"key1", @"key2"]]; // returns NSSharedKeySet NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithSharedKeySet:sharedKeySet]; dict[@"key1"] = @"Eezy"; dict[@"key2"] = @"Tutorials"; //We can an object thats not in the shared keyset dict[@"key3"] = @"Website"; NSLog(@"%@",dict);
Output
2014-03-22 13:19:09.686 iOS-Tutorial[929:a0b] { key1 = Eezy; key2 = Tutorials; key3 = Website; }
Adding Entries to a Mutable Dictionary
- setObject:forKey:
Adds a given key-value pair to the dictionary.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"Eezy" forKey:@"Key1"]; NSLog(@"%@",dict);
Output
2014-03-22 18:25:46.035 iOS-Tutorial[2745:a0b] { Key1 = Eezy; }
- setObject:forKeyedSubscript:
Adds a given key-value pair to the dictionary.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"Eezy" forKeyedSubscript:@"Key1"]; NSLog(@"%@",dict);
Output
2014-03-22 18:29:26.543 iOS-Tutorial[2761:a0b] { Key1 = Eezy; }
- setValue:forKey:
Adds a given key-value pair to the dictionary.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setValue:@"Eezy" forKey:@"Key1"]; NSLog(@"%@",dict);
Output
2014-03-22 18:30:34.190 iOS-Tutorial[2775:a0b] { Key1 = Eezy; }
- addEntriesFromDictionary:
Adds to the receiving dictionary the entries from another dictionary.
Example
NSDictionary *tempDictionary = @{@"key1":@"Eezy",@"key2": @"Tutorials"}; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict addEntriesFromDictionary:tempDictionary]; NSLog(@"%@",dict);
Output
2014-03-22 18:31:57.039 iOS-Tutorial[2788:a0b] { key1 = Eezy; key2 = Tutorials; }
- setDictionary:
Sets the contents of the receiving dictionary to entries in a given dictionary.
Example
NSDictionary *tempDictionary = @{@"key1":@"Eezy",@"key2": @"Tutorials"}; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setDictionary:tempDictionary]; NSLog(@"%@",dict);
Output
2014-03-22 18:31:57.039 iOS-Tutorial[2788:a0b] { key1 = Eezy; key2 = Tutorials; }
Removing Entries From a Mutable Dictionary
- removeObjectForKey:
Removes a given key and its associated value from the dictionary.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"key1":@"Eezy",@"key2": @"Tutorials"}]; [dict removeObjectForKey:@"key1"]; NSLog(@"%@",dict);
Output
2014-03-22 18:34:20.150 iOS-Tutorial[2814:a0b] { key2 = Tutorials; }
- removeAllObjects
Empties the dictionary of its entries.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"key1":@"Eezy",@"key2": @"Tutorials"}]; [dict removeAllObjects]; NSLog(@"%@",dict);
Output
2014-03-22 18:34:57.068 iOS-Tutorial[2827:a0b] { }
- removeObjectsForKeys:
Removes from the dictionary entries specified by elements in a given array.
Example
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"key1":@"Eezy",@"key2": @"Tutorials"}]; [dict removeObjectsForKeys:@[@"key1"]]; NSLog(@"%@",dict);
Output
2014-03-22 18:35:54.197 iOS-Tutorial[2838:a0b] { key2 = Tutorials; }
Advertisements