C、C++以及Objective-C添加的特性block

block

Blocks areoften used to simplify common tasks such as collection enumeration, sorting andtesting. They also make it easy to schedule tasks for concurrent orasynchronous execution using technologies like Grand Central Dispatch (GCD).

声明block:

void(^simpleBlock)(void);

 

定义block:

simpleBlock= ^{

        NSLog(@"This is a block");

};

 

声明和定义在一起:

void(^simpleBlock)(void) = ^{

    NSLog(@"Thisis a block");

};

 

调用block:

simpleBlock();

 

带返回值和参数的block:

double(^multiplyTwoValues)(double, double) =

                              ^(doublefirstValue, double secondValue) {

                                  returnfirstValue * secondValue;

                              };

 

    double result = multiplyTwoValues(2,4);

 

    NSLog(@"The result is %f",result);

If you needto be able to change the value of a captured variable from within a block, youcan use the__block storage type modifier on theoriginal variable declaration.

如果block是递归调用的,必须设置成 __block。e.g.

__blockint (^recursiveBlock)(int) = ^(int param) {

        if (param ==1) {

            return 1;

        }

        return (param--) * recursiveBlock(param);

};

 

recursiveBlock(5);  // 计算5的阶乘

 

You Can Pass Blocks as Arguments toMethods or Functions

-(void)beginTaskWithCallbackBlock:(void (^)(void))callbackBlock;

The (void (^)(void)) specifies that theparameter is a block that doesn’t take any arguments or return any values. Theimplementation of the method can invoke the block in the usual way:

- (void)beginTaskWithCallbackBlock:(void(^)(void))callbackBlock {

    ...

    callbackBlock();

}

ABlock Should Always Be the Last Argument to a Method

Objects Use Properties to Keep Track ofBlocks

The syntax to define a property to keeptrack of a block is similar to a block variable:

@interfaceXYZObject : NSObject

@property (copy) void (^blockProperty)(void);

@end

Note:You should specify copy as the property attribute, because a block needs to becopied to keep track of its captured state outside of the original scope.

A block property is set or invoked likeany other block variable:

    self.blockProperty = ^{

        ...

    };

    self.blockProperty();

typedef void(^BlockCC)(void);

 

// e.g.更新myTableview并显示最后一行

if (dataArray.count >0) {

        [myTableViewreloadData];  

        // reloadData后不能直接调用scrollToRowAtIndexPath,有可能lastIndex在table中不存在,

      // 所以要[NSObjectperformBlock:afterDelay:0];

        [myTableViewretain];  //防止关闭视图,myTableView释放之后scrollToBottom出错

        NSIndexPath*lastIndex = [NSIndexPathindexPathForRow:dataArray.count-1inSection:0];

        void(^scrollToBottom)(void) = ^{

            [myTableViewscrollToRowAtIndexPath:lastIndexatScrollPosition:UITableViewScrollPositionBottomanimated:YES];

            [myTableViewrelease];

        };

       

        [NSObjectperformBlock:scrollToBottomafterDelay:0];

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值