存储对象都必须是id(对象类型)不能使基础类型
NSDictionary *scores=[[NSDictionary alloc]initWithObjectsAndKeys:@"89",@"english",@"70",@"computer",nil];
*scores=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:89],@"english"];
scores=[NSDictionary dictionaryWithObjectsAndKeys:]用法与initWithObjectsAndKeys;
NSNumber *englishScore=[scores objectForKey:@"english"];
集合的遍历
方法一
for(NSString *key int socres)
{
// 通过每个元素的key访问value
NSLog(@"%@:%d",key,[[score objetForKey:key] intValue]);
}
方法二
[socres enumerateKeysAndObjectsUsingBloc:^(id key, id obj,BOOL *stop)
{
NSNumber *num=(NSNumber *)obj;
NSLog(@"%@:%d",key,[num intValue]);
}
方法三
NSArray *keysArray = [scores allKeys];
for(int i=0; i<[scores count]; i++)
{
NSLog(@"%@:%d",[keysArray objectAtIndex:i);
NSLog(@"")----;
}
字典排序
NSArray *keysArray=[scores keysSortedByValueUsingSelector:@selector(compare:)];