blocks 学习

转载请注明,谢谢。

 

Ios4已经直接支持blocks,很有必要学习一下。

iosblocks是对象,它封装了一段代码,这段代码可以在任何时候执行。Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值。它和传统的函数指针很类似,但是有区别:blocksinline的,并且它对局部变量是只读的。

Blocks的定义:

         int (^Multiply)(int, int) = ^(int num1, int num2) {return num1 * num2;};

定义了一个Multiplyblocks对象,它带有两个int参数,返回int。等式右边就是blocks的具体实现,注意{}blocks体里的;。

Blocks可以访问局部变量,但是不能修改。

              int multiplier = 7;

     int (^myBlock)(int) = ^(int num) {

         multiplier ++;//编译报错

         return num * multiplier;

     };

如果要修改就要加关键字:__block

         __block int multiplier = 7;

     int (^myBlock)(int) = ^(int num) {

         multiplier ++;//这样就可以了

         return num * multiplier;

     };

 

作为函数的参数,blocks某种意义上替代了回调函数或者delegate。当函数调用了,假设某个事件触发,这时blocks里的内容就会运行。这样有利于代码的整合和阅读,你不需要到处去实现委托方法了。

系统API中已经有很多支持blocks参数了

·       Completion handlers

·       Notification handlers

·       Error handlers

·       Enumeration

·       View animation and transitions

·       Sorting

例如:

[UIView animateWithDuration:(NSTimeInterval)duration animations:(void (^)())animations]

集合体中也可以运用blocks。枚举一个数组时我们通常:

         for (id obj in Array);

现在,

NSString *area = @"Europe";

    NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];

    NSMutableArray *areaArray = [NSMutableArray arrayWithCapacity:1];

    NSIndexSet *areaIndexes = [timeZoneNames indexesOfObjectsWithOptions:NSEnumerationConcurrent

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

                                                                 NSString  *tmpStr = (NSString *)obj;

                                                                 return [tmpStr hasPrefix:area];

                                                             }];

   

    NSArray *tmpArray = [timeZoneNames objectsAtIndexes:areaIndexes];

    [tmpArray enumerateObjectsWithOptions:NSEnumerationConcurrent|NSEnumerationReverse

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

                                   [areaArray addObject:[obj substringFromIndex:[area length]+1]];

                               }];

    NSLog(@"Cities in %@ time zone:%@", area, areaArray);

blocksobj就是数组中的每个成员,我们就可以在blocks内对每个对象进行处理。再比如:

NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"a",@"b",@"abc",nil];

    NSMutableArray *mArrayCount = [NSMutableArray arrayWithCapacity:1];

    [mArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock: ^(id obj,NSUInteger idx, BOOL *stop){

        [mArrayCount addObject:[NSNumber numberWithInt:[obj length]]];

    }];

   

    NSLog(@"%@",mArrayCount);

你会发现,这样写代码更容易读懂。

最后,看一个排序的例子:

    NSArray *stringsArray = [NSArray arrayWithObjects:

                             @"string 1",

                             @"String 21",

                             @"string 12",

                             @"String 11",

                             @"String 02", nil];

    static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |

    NSWidthInsensitiveSearch | NSForcedOrderingSearch;

    NSLocale *currentLocale = [NSLocale currentLocale];

    NSComparator finderSort = ^(id string1, id string2) {

        NSRange string1Range = NSMakeRange(0, [string1 length]);

        return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];

    };

    NSLog(@"finderSort: %@", [stringsArray sortedArrayUsingComparator:finderSort]);

结果:finderSort: (

    "string 1",

    "String 02",

    "String 11",

    "string 12",

    "String 21"

)

posted on 2011-01-21 12:08  scorpiozj  阅读(11343)  评论(8编辑  收藏  举报