block作为函数参数回调

Blocks make this much easier, however, because you can define the callback behavior at the time you initiate the task, like this:

- (IBAction)fetchRemoteInformation:(id)sender {
    [self showProgressIndicator];
 
    XYZWebTask *task = ...
 
    [task beginTaskWithCallbackBlock:^{
        [self hideProgressIndicator];
    }];
}
This example calls a method to display the progress indicator, then creates the task and tells it to start. The callback block specifies the code to be executed once the task completes; in this case, it simply calls a method to hide the progress indicator. Note that this callback block captures self in order to be able to call the hideProgressIndicator method when invoked. It’s important to take care when capturing self because it’s easy to create a strong reference cycle, as described later in Avoid Strong Reference Cycles when Capturing self.

In terms of code readability, the block makes it easy to see in one place exactly what will happen before and after the task completes, avoiding the need to trace through delegate methods to find out what’s going to happen.

The declaration for the beginTaskWithCallbackBlock: method shown in this example would look like this:

- (void)beginTaskWithCallbackBlock:(void (^)(void))callbackBlock;
The (void (^)(void)) specifies that the parameter is a block that doesn’t take any arguments or return any values. The implementation of the method can invoke the block in the usual way:

- (void)beginTaskWithCallbackBlock:(void (^)(void))callbackBlock {
    ...
    callbackBlock();
}
Method parameters that expect a block with one or more arguments are specified in the same way as with a block variable:

- (void)doSomethingWithBlock:(void (^)(double, double))block {
    ...
    block(21.0, 2.0);
}
A Block Should Always Be the Last Argument to a Method

It’s best practice to use only one block argument to a method. If the method also needs other non-block arguments, the block should come last:

- (void)beginTaskWithName:(NSString *)name completion:(void(^)(void))callback;
This makes the method call easier to read when specifying the block inline, like this:

[self beginTaskWithName:@"MyTask" completion:^{
        NSLog(@"The task is complete");
    }];

时间: 2024-08-29 20:42:31

block作为函数参数回调的相关文章

Block、委托、回调函数原理剖析(在Object C语境)——这样讲还不懂,根本不可能!

开篇:要想理解Block和委托,最快的方法是搞明白“回调函数”这个概念. 做为初级选手,我们把Block.委托.回调函数,视为同一原理的三种不同名称.也就是说,现在,我们把这三个名词当成一回事.在这篇文章内,Block就是回调函数,委托也是回调函数,不再作详细的区分了.OK,Action! 那么,什么是回调函数?“回调”概念的主语是谁? 举个栗子(伪代码): 首先有个类,我们姑且称之为A类吧. A.h 文件 //声明回调函数:给指定的员工发放工资 -(void)paySalaryForStaff

新手,对函数,函数指针,回调函数, 函数指针作为函数的返回值和block的一些见解

很多初学者,学c语言时,看到函数都觉得难,我也是,一开始觉得函数太难了,后来慢慢就理解了 第一:函数 在c语言中最简单的函数一般有四种: 1, 无返回值, 无参数. 2, 无返回值,有参数. 3, 有返回值,无参数. 4, 有返回值,有参数 1, 无返无参  定义方式:      void 函数名(); 一般不怎么用这种形式的函数,可用来输出,打印 void functionOne() { printf("无返回值,无参数"); // 没有返回值不能写return } int main

关于Array的map方法中回调函数参数的问题

开门见山,我们先来看两个例子. var arr=['1','4','9','16']; var r=arr.map(Math.sqrt); 猜猜r的结果会是多少? 没错就是 [1,2,3,4] 我们再来试试另一个, var arr=['1','4','9','16']; var r=arr.map(parseInt); 再猜猜结果是多少? 是[1,2,3,4]? console试一试看看结果是多少 [1,NaN,NaN,1]! 是不是大吃一惊 其实真相---- 就是参数个数搞的鬼! 让我们再来看

速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数

[源码下载] 作者:webabcd 介绍速战速决 之 PHP 函数基础 函数参数 函数返回值 可变函数 匿名函数 闭包函数 回调函数 示例1.函数的相关知识点 1(基础)function/function1.php <?php /** * 函数的相关知识点 1(基础) */ // 可以在相关的 function 声明语句之前调用该函数 f1(); function f1() { echo "f1"; echo "<br />"; } // 这里调用

15) .实现可变参数的(成员/非成员)模板函数的回调,消除类似代码

完整的源代码及用法测试代码可以在我的资源中去下载. C++语法不支持模板函数/模板成员函数作为回调函数,同时把运行期代码向编译期代码转换也只有switch...case或者if..else能够实现. 如果case比较多的时候,代码非常臃肿,而且类似的大片代码中,往往只有一个参数的不同,其它都是相同的.这对于用户来说,都是一个大量的重复性的hard-code性的负担,而且也容易导致出错. 本库采用了封装,可以把运行性的switch...case/if..else分支选择转换为编译期的代码,支持模板

Node.js学习笔记(3)——关于回调函数和函数的回调

说明:本人是node.js的初学者,尝试向别人解释这是怎么回事是自我学习的一个好方法.如果你发现有些地方并不是那么正确,欢迎提出来让我知道以便修正,共同进步,谢过^_^.       欢迎交流,本人微博:http://weibo.com/bitsea  很多地方都涉及到函数的回调,在这里简单说一下什么是函数的回调. 回调函数就是回来再调用的函数. 基于js的单线程执行代码的风格,回调是必须的选择.也可以说是一种不得已而为之的选择吧,回调无疑增加了代码的复杂性,使其变得难读.难理解,难维护.但是,

函数的回调

回调函数 关于回调函数,大白话总结!不一定全,但是绝对易懂. 第1个问题:什么是回调函数? 回调函数,本质上也是个函数(搁置函数和方法的争议,就当这二者是 一回事).由“声明”.“实现”.“调用”三部分组成. 在上面的例子中,我可以看出,函数amount(其实是Block),的声明 和调用在A类中,而实现部分在B类中.也就是说,B类实现了amount 函数,但并没有权限调用,最终还是 由A类触发调用.我们称这样的机 制为“回调”.意思是“虽然函数的实现写在B类中,但是真正的调用还是 得由A类来完

Objective-C Block与函数指针比较

相似点 1.函数指针和Block都可以实现回调的操作,声明上也很相似,实现上都可以看成是一个代码片段. 2.函数指针类型和Block类型都可以作为变量和函数参数的类型.(typedef定义别名之后,这个别名就是一个类型) 不同点 1.函数指针只能指向预先定义好的函数代码块(可以是其他文件里面定义,通过函数参数动态传入的),函数地址是在编译链接时就已经确定好的. 2.Block本质是Objective-C对象,是NSObject的子类,可以接收消息. 3.函数里面只能访问全局变量,而Block代码

Java/Android中的函数调用&amp;回调函数&amp;自定义回调函数

在做Android自定义控件时遇到要自定义回调函数的问题,想想自己还暂时没有那么精深的技术,赶紧返过头回来再重新研究Java中回调函数的问题.然而不幸的是,网上太多杂乱的帖子和博客都是转来转去,而且都是那一篇"C中的回调函数.....指针.....java....",一点看不出来是自己的思路,估计都是哪哪哪抄来的!(呵呵,要么就是吐槽对了,要么就是我水平太烂读不懂还妄加评论)还有一些很不错的文章,我会在最后参考中加上链接,大家可以看看. 那么来开始我们的正题--什么是回调函数? 我们一