Objc block系列一:使用block简化collection遍历

前言

Block存在已久,Mac OSX10.6和iOS4.0以上系统都可以使用。 昨天偶然间看了一下Apple的官方文档,有一点恍然大悟的感觉:原来block还能这么用,惭愧惭愧。

原文: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW1



使用block简化collection遍历

1. 先初始化一个array,备用:

    NSMutableArray* numbers = [NSMutableArray array];

    for (int i=0; i<10; i++) {

        [numbers addObject:[NSNumber numberWithInt:i]];

    }


2. 通常遍历这个array,我会这么写:

    for (int i=0; i<[numberscount]; i++) {

        NSLog(@"The object for index %d is %@", i, [numbersobjectAtIndex:i]);

    }


因为,很久很久以前,在学校学c语言就是这么写。 想到for循环,这是第一个出现在大脑中的模式。可能每个学过C语言的人,都有这个情节,就像是你的初恋。

3. 后来,遇到了 for ... in ,偶尔也会第一个想到她。

    int i=0;

   for (NSObject* aobjin numbers) {

        NSLog(@"The object for index %d is %@", i++, aobj);

    }


但内心深处总有些不踏实。因为,难免,有时脑子会开个小差,在循环中做一些别的事情:

    int i=0;

   for (NSNumber* aobjin numbers) {

        NSLog(@"The object for index %d is %@", i++, aobj);

       if ([aobjisEqual:[NSNumbernumberWithInt:3]]) {

            [numbersremoveObjectAtIndex:i];

        }

    }


这时候就会收到一大堆的抱怨:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0xa1516c0> was mutated while being enumerated.'


*** First throw call stack:

(

0   CoreFoundation                      0x017ee1e4 __exceptionPreprocess + 180

1   libobjc.A.dylib                     0x0156d8e5 objc_exception_throw + 44

2   CoreFoundation                      0x0187dcf5 __NSFastEnumerationMutationHandler + 165

3   HelloBlks                           0x0000327b -[ViewController enumerateBlock] + 667

4   HelloBlks                           0x00002f22 -[ViewController setupUi] + 146

5   HelloBlks                           0x00002e38 -[ViewController viewDidLoad] + 88

...

...

libc++abi.dylib: terminating with uncaught exception of type NSException


天哪,又忘了,for in 中是不能修改数组的。


4. 终于有一天,我们认识了block

    [numbersenumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop) {

        NSLog(@"The object for index %d is %@", idx, obj);

    }];


说实在的,真的没有一见钟情的感觉,真的没有。 看到这个函数签名就把我吓退了,恐怕写上100遍也记不住。不过,我还是努力的尝试说服了自己:写一遍试一试,就写一遍好不好?

很不情愿的在Xcode中输入了 numbers emu,这时提示出现了,按一下回车,再按一下回车,嗯,还不错:

    [numbers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        <#code#>

    }]

   不到十个字母,再加上两个回车。


最重要的是,当我这么写的时候,居然没有听到一句抱怨:

    [numbersenumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) {

        NSLog(@"The object for index %d is %@", idx, obj);

       if ([objisEqual:[NSNumbernumberWithInt:3]]) {

            [numbersremoveObjectAtIndex:idx];

        }

    }];



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值