Objective-C中3种枚举比较及KVC两个小技巧
技巧 比较 两个 枚举 Objective KVC
2023-09-11 14:15:06 时间
Objective-C中3种枚举比较及KVO两个小技巧
一:oc的3种枚举
- for循环
- for in
- 枚举块
如代码
NSUInteger totalCount = 10000; NSMutableArray *array = [NSMutableArray arrayWithCapacity:totalCount]; //create an array including 10000 elements for (int i = 0; i<totalCount; i++) { array[i] = [@(i) stringValue]; } //C Language For Loop Enumeration CFTimeInterval start1 = CACurrentMediaTime(); for (int j = 0; j< totalCount; j++) { NSLog(@"%@",array[j]); } CFTimeInterval end1 = CACurrentMediaTime(); NSLog(@"C Language For Loop method %f",end1 - start1); //Objective-C Fast Enumeration CFTimeInterval start2 = CACurrentMediaTime(); for (NSString *string in array) { NSLog(@"%@",string); } CFTimeInterval end2 = CACurrentMediaTime(); NSLog(@"Objective-C Fast Enumeration method %f",end2 - start2); //Objective-C Block Enumeration CFTimeInterval start3 = CACurrentMediaTime(); [array enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSString * obj, NSUInteger idx, BOOL *stop) { NSLog(@"%@",obj); }]; CFTimeInterval end3 = CACurrentMediaTime(); NSLog(@"Objective-C Block Enumeration method %f",end3 - start3);
打印结果:
综上:块是最快的!
二:kvc两个小技巧
1:keyPath取嵌套字典的值;
2: kvc消息的高级传递
转自:http://nijino.cn/blog/2013/07/02/using-kvc/
http://nijino.cn/blog/2014/03/11/comparison-for-3-enumeration-methods-in-objective-c/
相关文章
- ASP.NET比较常用的26个性能优化技巧
- Python 编程知识与技巧
- BZOJ4707 : B君的技巧
- Jupyter Notebook的27个窍门,技巧和快捷键
- 程序员装B小技巧——管理你的桌面
- 《树莓派实战秘籍》——1.3 技巧03解码LED灯
- Qt开发经验小技巧261-265
- Qt开发经验小技巧251-255
- laravel中及其常用的一些函数方法(自己看)和技巧(不断添加中)
- Linux系统小技巧(2):利用ps工具统计CPU/MEM消耗高或者负载高的进程
- 51定时器配置PWM的技巧
- 呼叫中心客服人员招募策略与面谈技巧
- WebBrowser 显示Html内容3点细节技巧
- CAD引线文字在上方怎么设置?CAD引线标注设置技巧
- VIM使用技巧15