Xcode auto-complete for blocks-within-a-block (and the blocks they're in…)

前端 未结 1 648
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 16:38

How do you set up a block property that takes as an argument another block property so that auto-complete supplies all of the required parameters for both blocks?

To exp

相关标签:
1条回答
  • 2021-01-26 17:21

    The following code shows you how to set up blocks-within-a-block so that Xcode will auto-complete both the parameters and return values when you use the block:

    In the header file:

    typedef BOOL (^Condition)(void);
    typedef void (^Success)(void);
    typedef void (^Failure)(void);
    typedef void (^Task)(Condition condition, Success success, Failure failure);
    
    @property (copy, nonatomic, readwrite) Task task;
    @property (copy, nonatomic, readwrite) Condition condition;
    @property (copy, nonatomic, readwrite) Success success;
    @property (copy, nonatomic, readwrite) Failure failure;
    

    In the implementation file:

    - (Task)task
    {
        return ^(Condition condition, Success success, Failure failure) {
            if (condition())
            {
                success();
            } else {
                failure();
            }
        };
    }
    

    In the implementation file of any class granted access to the Task property, being typing the path to the property, as well as the property name itself, until Xcode auto-completes the rest:

    AppServices.task(<#^BOOL(void)condition#>, <#^(void)success#>, <#^(void)failure#>)
    

    Press the tab key to advance to the first parameter (condition), and then press Return; repeat for the remaining two parameters (success and failure):

    AppServices.task(^BOOL{
            <#code#>
        }, ^{
            <#code#>
        }, ^{
            <#code#>
        })
    

    Replace code with your code, making sure to return the appropriate value for any block that returns a non-void type )(condition returns BOOL):

    [class].task(^BOOL{
            return TRUE;
        }, ^{
            NSLog(@"TRUE");
        }, ^{
            NSLog(@"FALSE");
        });
    

    In this example, the Task block executes either the Success or Failure block depending on the return value of the conditional specified in the Condition block.

    My intended use is far more complex and pragmatic than this example; but, as far as "how to do it" goes, it will do.

    0 讨论(0)
提交回复
热议问题