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

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/

转载于:https://www.cnblogs.com/cocoajin/p/3607598.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值