欢迎使用CSDN-markdown编辑器

block

代码

    int multiplier = 7;
    int (^myBlock)(int) = ^(int num) {
        return num * multiplier;
    };


    //
    char *myCharacters[3] = {"TomJohn","George","Charles Condomine"};

    gsort_b(myCharacters,3,sizeof(char *),^(const void *l,const void *r) {
        char *left = *(char **)l;
        char *right = *(char **)r;

        return strncmp(left, right, 1);
    });

    NSArray *stringsArray = @[@"string 1",
                              @"string 21",
                              @"string 12",
                              @"string 11",
                              @"string 02"];

    static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch;
    NSLocale *currentLocale = [NSLocale currentLocale];

    __block NSUInteger orderedSameCount = 0;

    NSComparator finderSortBlock = ^(id string1, id string2) {
        NSRange string1Range = NSMakeRange(0, [string1 length]);
        NSComparisonResult comparisonResult = [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
        if (comparisonResult == NSOrderedSame) {
            orderedSameCount++;
        }
        return comparisonResult;
    };
    NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:finderSortBlock];


    void (^blockReturningVoidWithVoidArgument)(void);
    int (^blockReturningIntWithIntAndCharArguments)(int,char);
    void (^arrayOfTenBlockReturningVoidWithIntArgument)(int);

    typedef float (^MyBlockType)(float, float);

    MyBlockType myFirstBlock;
    MyBlockType t ;

    float (^oneFrom)(float);
    oneFrom = ^(float aFloat) {
        float result = aFloat - 1.0;
        return result;
    };

参考:

1.http://www.tuicool.com/articles/3EFBry

2.https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40007502-CH1-SW1
3.http://zhidao.baidu.com/link?url=OFHE6SQg5IsPTtSIDBFuynwBxND7YzlV0u5O8cxWWDIRsJTsFYree6Dtcd-E1AO7-WekiWAZh8UA2X_tWz_xEemCSLnfp2NhlZ_Duekl2oa
4.http://blog.csdn.net/jasonblog/article/details/7756763#comments

你可能感兴趣的:(ios)