zoukankan      html  css  js  c++  java
  • 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");
        }];


  • 相关阅读:
    windows C++ 获得一个进程的线程数目
    乌龟 SVN 在update后,如何知道 update前的版本号?
    C++ 类中的static对象貌似不能是类对象
    WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
    用Remoting 实现一个文件传输组件
    [转载]Extreme Game Programming
    C#:10进制转2进制函数
    bindows 源码格式化工具(测试中的测试)C#源码
    明天又是周末,可以好好休息了!
    将文件加入到图形文件里;
  • 原文地址:https://www.cnblogs.com/NSNULL/p/4482350.html
Copyright © 2011-2022 走看看