dictionaryWithObjectsAndKeys 用来初始化
objectForKey 用来取值
sample:
NSDictionary *testDic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"hello",@"2",@"what", nil];
NSLog(@"val is %@",[testDic objectForKey:@"hello"]);
NSMutableDictionary可变字典NSMutableDictionary继承自NSDictionary
常用api:
dictionaryWithCapacity: 用来初始化
setObject: 设置
removeObjectForKey 删除
[count] 返回字典的数量
[allKeys]获取所有键的集合
[allValues】获取所有值得集合
sample:
NSMutableDictionary *testDic=[NSMutableDictionary dictionaryWithCapacity:12]; [testDic setObject:@"1" forKey:@"hel"]; [testDic setObject:@"2da" forKey:@"what"]; [testDic setObject:@"hello" forKey:@"hello"]; NSLog(@"the val is %@",[testDic objectForKey:@"what"]); NSLog(@"the length is %ld",[testDic count]); [testDic removeObjectForKey:@"hello"]; NSLog(@"the length is %lu",testDic.count); NSArray *keyArray=[testDic allKeys]; NSArray *valArray=[testDic allValues]; NSLog(@"the all keys are %@",keyArray); NSLog(@"the all values are %@",valArray); }
返回结果是:
2013-10-09 17:20:19.533 element[401:303] the val is 2da 2013-10-09 17:20:19.535 element[401:303] the length is 3 2013-10-09 17:20:19.536 element[401:303] the length is 2 2013-10-09 17:20:19.537 element[401:303] the all keys are ( what, hel ) 2013-10-09 17:20:19.537 element[401:303] the all values are ( 2da, 1 )
不经常用的方法为
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
- (void)removeAllObjects;
- (void)removeObjectsForKeys:(NSArray *)keyArray;
- (void)setDictionary:(NSDictionary *)otherDictionary;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)keyNS_AVAILABLE(10_8,
6_0);
sample如下:
NSMutableDictionary *emptyDic=[NSMutableDictionary dictionary]; [emptyDic setValue:@"2" forKey:@"2"]; NSLog(@"empty DIC is %@",emptyDic); NSMutableDictionary *testDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"hello",@"1",@"andy",@"2",@"yang",@"3",nil]; NSDictionary *dic4=[NSDictionary dictionaryWithObject:@"yang" forKey:@"4"]; [testDic addEntriesFromDictionary:dic4]; NSLog(@"the mutalble dictionary is %@",testDic); [emptyDic setDictionary:testDic]; NSLog(@"empty DIC is %@",emptyDic); NSArray *deleteArr=[NSArray arrayWithObjects:@"1",@"2",@"yang",nil]; [testDic removeObjectsForKeys:deleteArr]; NSLog(@"the mutalble dictionary is %@",testDic); [testDic removeAllObjects]; NSLog(@"the mutalble dictionary is %@",testDic);
结果:
2013-10-09 17:39:37.832 element[463:303] empty DIC is { 2 = 2; } 2013-10-09 17:39:37.834 element[463:303] the mutalble dictionary is { 1 = hello; 2 = andy; 3 = yang; 4 = yang; } 2013-10-09 17:39:37.834 element[463:303] empty DIC is { 1 = hello; 2 = andy; 3 = yang; 4 = yang; } 2013-10-09 17:39:37.835 element[463:303] the mutalble dictionary is { 3 = yang; 4 = yang; } 2013-10-09 17:39:37.835 element[463:303] the mutalble dictionary is { }
遍历字典的方法:
for(id key in dic)
{
id obj=[dic objectForKey:key];
NSLog(@"%@",obj);
}
一般的枚举
NSMutableDictionary *testDic=[NSMutableDictionary
dictionaryWithObjectsAndKeys:@"hello",@"1",@"andy",@"2",@"yang",@"3",nil];
for(int index=0;index<[testDic
count];index++)
{
NSString *obj=[testDic
objectForKey:[[testDic allKeys] objectAtIndex:index]];
NSLog(@"the val is %@",obj);
}
for(id key
in testDic)
{
NSString *obj2=[testDic
objectForKey:key];
NSLog(@"the val is %@",obj2);
}