標籤:
儲存物件都必須是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:)];
Object-C-NSDictionary