NSMutableDictionary 用于处理值对集合。保存到键的对象必须实现了copyWithZone:方法。和NSMutableArray一样添加到容器时对象收到retain消息,把对象从容器中移除时收到release消息,任何想在移除对象还要对该对象进行操作必须先对该对象发出一条retain消息,在使用完成后需要release。
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//创建Dictionary,Capacity仅控制初始化大小
NSMutableDictionary *fruits = [NSMutableDictionary dictionaryWithCapacity:2];
[fruits setObject:@"Orange" forKey:@"OrangeKey"];
[fruits setObject:@"Apple" forKey:@"AppleKey"];
[fruits setObject:@"Blueberry" forKey:@"BlueberryKey"];
NSLog(@"%@", fruits);
//获取一个对象
id oneObj = [fruits objectForKey:@"AppleKey"];
NSLog(@"%@", oneObj);
//如果找不到应的条目,返加nil
id noObj = [fruits objectForKey:@"aaa"];
NSLog(@"%@", noObj);
[pool drain];
return 0;
}