Object-C-NSDictionary
2015-06-08 10:56
483 查看
存储对象都必须是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:)];
View Code
ie
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:)];
字典: //key 一般使用字符串 //value 可以使用任意对象类型 // NSDictionary *scores = [[NSDictionary alloc]initWithObjectsAndKeys:@"89",@"english",@"70",@"computer", nil]; // // NSLog(@"%@",scores); NSDictionary *scores2=[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:89],@"english", [NSNumber numberWithInt:92],@"maths", [NSNumber numberWithInt:70],@"computer" , nil]; // NSLog(@"%ld",[scores2 count]); // NSLog(@"%@",scores2); NSNumber *englishScores = [scores2 objectForKey:@"english"]; NSLog(@"%@",englishScores); NSLog(@"%d",[englishScores intValue]); // //遍历方法1:for in for (NSString *key in scores2) { //通过每个元素的key访问value NSLog(@"%@:%d",key,[[scores2 objectForKey:key] intValue]); } // //遍历方法2:for NSArray *keysArray = [scores2 allKeys]; for (int i=0; i<[scores2 count]; i++) { NSString *key = [keysArray objectAtIndex:i]; NSLog(@"%@:%d",key,[[scores2 objectForKey:key]intValue]); } // //排序 NSArray *keys = [scores2 keysSortedByValueUsingSelector:@selector(compare:)]; NSLog(@"%@",keys); } return 0;
View Code
ie
相关文章推荐
- Objective-C关键字self和super详解
- GitHub 上排名前 100 的 Objective-C 项目简介
- Objective-C Autorelease Pool 的实现原理
- 使用 powerdesigner 15的Object Oriented Model生成 nhibernate实体类,映射文件和数据访问层
- Objective-C单例模式实现
- Objective-c set方法,get方法
- Objective-C 字典NSDictionary用法
- Objective-c 装箱和拆箱
- 类的内部是否应该直接访问实例变量
- 阅读《Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法》总结
- [Objective-C] 005_Category(类别)
- 理解“属性”
- 十分钟让你明白Objective-C的语法(和Java、C++的对比)
- CVPR2014 Objectness源码转换(完整版) VS2012 X64 –>win32
- object_c id 与 instancetype
- object-c面向对象2
- Object-C中的一些常见问题及解答
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- objective-c 异常处理
- Objective C 中的nil,Nil,NULL和NSNull理解