The dictionary and the array and string also have a variable subclass, but the method names involved are different, but the principles are similar.
Methods involved:
Removeobjectforkey:Delete a specified key-Value Pair (PS: only the key is entered here. The key and value are deleted, but nothing happens to the input value ).
Let's take a look at the example below:
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableDictionary *dicti = [NSMutableDictionary new]; [dicti setObject:@"Five" forKey:@"5"]; [dicti setObject:@"Four" forKey:@"4"]; //添加指定的键值对// [dicti setObject:@"Five" forKey:@"5"]; //添加键值对和删除键值对的时候没有说是插入到哪里, 和删除哪一个, 因为字典中的键值对是不讲究位置的. NSLog(@"dicti = %@", dicti); //removeObjectForKey:删除对应的键值对. //删除键值对也是同样不将就顺序.// [dicti removeObjectForKey:@"4"]; [dicti removeObjectForKey:@"5"]; NSLog(@"diciti = %@", dicti); [dicti release]; } return 0;}
Output result:
2014-10-13 21:16:31.843 NSMutableDictionary[6749:303] dicti = { 4 = Four; 5 = Five;}2014-10-13 21:16:31.844 NSMutableDictionary[6749:303] diciti = { 4 = Four;}Program ended with exit code: 0
How to Use nsmutabledictionary