Objective-C中3种枚举比较及KVC两个小技巧

Objective-C中3种枚举比较及KVO两个小技巧

一:oc的3种枚举

  • for循环
  • for in
  • 枚举块

   如代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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/

posted @   cocoajin  阅读(790)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示