NSDictionary用于存储key-value的数据结构,与JAVA中的Map类似。
EG:
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"Apple",@"A",@"Google",@"G",nil];
可变长度的字典:NSMutableDictionary
NSMutableDictionary *mDic=[NSMutableDictionary dictionaryWithCapacity:10];
[mDic setObject:@"Apple" forKey:@"A"];//添加value-key对
[mDic setObject:@"Windows Phone 7" forKey:@"W"];
[mDic removeObjectForKey:@"W"];//移除指定key的value
//快速迭代的for-each循环
NSEnumerator *keyEnum=[mDic keyEnumerator];//获得key的枚举器
id key;
while(key=[keyEnum nextObject]){
NSLog(@"%@:%@",key,[mDic objectForKey:key]);
}
|
|