首页投稿(暂停使用,暂停投稿)

Objective-C的Block浅析

2016-04-20  本文已影响56人  油菜小白
  • 本文的写作目的是为学习记录,同时分享给大家,希望大神能够对文中错误的理解进行指正。

1 了解Block

1.1 概念

1.2 语法

^【返回值类型】【(参数列表)】表达式

其中“【】”内的内容为可省略内容。

例如:

^void (id testObj) {
    testObj = [[NSString alloc] initWithString:@"This is a iOS Block test!"];
    NSLog(@”%@”,testObj);
}

2 Block的用法

2.1 Block类型变量

在Block语法下,可将Block代码块赋值给Block类型的变量。即整个Block代码块可以作为“值”赋值,赋值对象为声明为Block类型的变量。

2.1.1 以Block的返回值作为赋值对象

这种类型再熟悉不过,与普通的函数类似,将函数中return的值作为赋值对象。
例如:

int testInt = ^int {
    return 7;
};

2.1.2 以Block作为赋值对象

例如:

//声明Block类型变量
int (^testBlk)(int);
//声明Block类型变量,语法为“ 返回值类型 (^Block代码块名称)【(参数类型列表)】”
//将Block代码块赋值给Block类型变量
testBlk = ^(int count){
    return count + 1;
};

例如:

void testFunction(Bool testBool, int (^testBlk)(int)){ 
    if (testBool) {
        testBlk;
    }
}

例如:

int (^testBlk)(int) testFunction(){
    return ^(int count) { return count + 1;};
}

例如:

//声明Block类型,作为Block的参数类型
typedef int (^testBlockType)(int);
int testInt = 0;
//作为传递参数时
void testFunction(testBlockType testBlk){
    testBlk = ^(int count) { return count + 1;};
    testInt = testBlk(10);
}
//作为返回值时
testBlockType testFunction(){
    return (^(int count) { return count + testInt;});
}

2.2 在Block中,截获自动变量

在Block的代码块中,截获代码块之外的变量,其变量值、使用方式须遵守Block语法的规则。

2.2.1截获自动变量

例如:

int testInt = 0;
void (^blk)() = ^{printf(“value = %d”,testInt)}; 
blk();
//此处打印结果为:value = 0
testInt = 1;
blk();
//此处打印结果为:value = 0

例如:

int testInt = 0;
void (^blk)() = ^{ testInt = 1;}; 
blk();
//此处报错。在Block代码块中,无法改写非该Block内部自动变量的值。

2.2.2 __block说明符

如果需要在Block代码块中改写该Block之外的自动变量的值,需要在该自动变量上附加__block说明符。
例如:

__block int testInt = 0;
void (^blk)() = ^{ testInt = 1;}; 
blk();

参考:

上一篇 下一篇

猜你喜欢

热点阅读