Xcode自动完成一个块内的块(以及它们所在的块...) [英] Xcode auto-complete for blocks-within-a-block (and the blocks they're in...)

查看:78
本文介绍了Xcode自动完成一个块内的块(以及它们所在的块...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置一个将另一个块属性作为参数的块属性,以便自动完成功能为两个块提供所有必需的参数?

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?

为进一步解释,我将演示仅具有一个block属性的自动完成功能.

To explain further, I'll demonstrate how auto-complete works with just one block property.

AppDelegate.h中,为需要访问block属性的所有类创建引用AppDelegate类的便捷方法:

In AppDelegate.h, create a convenient way to reference the AppDelegate class for all classes that require access to the block property:

#define AppServices ((AppDelegate *)[[UIApplication sharedApplication] delegate])

然后,定义块:

typedef void (^LogEvent)(NSString *context, NSString *entry, LogTextAttributes logTextAttributes, dispatch_block_t block);

然后,将块指定为属性:

Then, designate the block as a property:

@property (copy, nonatomic, readwrite) LogEvent logEvent;

在AppDelegate.m中,编写一个返回??? n的方法(我不确切知道我要返回什么;我只知道它可以工作-是它的一个块:

In the AppDelegate.m, write a method that returns ???n (I don't know exactly what I'm returning; I just know that it works—is it a block that :

- (LogEvent)logEvent
{
    return ^(NSString *context, NSString *entry, LogTextAttributes logTextAttributes, dispatch_block_t block) {
       ...task A...
       block();
    };
}

在另一个类文件中,导入包含block属性的标头:

In another class file, import the header containing the block property:

#import "AppDelegate.h"

然后,开始输入LogEvent块属性的名称(logEvent),并让自动完成功能填写所需的参数:

Then, begin typing the name of the LogEvent block property (logEvent), and let auto-complete fill in the required parameters:

AppServices.logEvent(<#NSString *context#>, <#NSString *entry#>, <#LogTextAttributes logTextAttributes#>, <#^(void)block#>)

要使用该功能,您必须将占位符替换为实际值,并且必须将block替换为:

To use that, you have to substitute the placeholders for real values, and you have to replace block with:

^{ ...block... }

这是一个真实场景中的示例:

Here's an example of what that looks like in a real-world scenario:

AppServices.logEvent([NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__], @"Starting network service browser...", LogTextAttributes_Operation, ^(){
    ...task B...
    });

这将执行任务A,然后执行任务B.我想做的是定义两个块,将两个块都指定为属性,然后将一个块添加到另一个块作为参数.然后,当我输入接受另一个作为参数的块的名称时,我希望Xcode为它们自动完成所需的参数.例如:

What this will do is perform task A, and then perform task B. What I want to do is define two blocks, designate both as properties, and then add one to the other as a parameter; then, when I type the name of the block that accepts the other as a parameter, I want Xcode to auto-complete the required parameters for both of them. For example:

对于名为RunTask的块属性,它使用名为blockdispatch_block_t作为参数,我希望在相同的设置下,键入RunTask会产生以下结果:

For a block property named RunTask that takes, as a parameter, a dispatch_block_t named block, I would expect, with the same setup, that typing RunTask would produce this:

RunTask(<#^(void)block#>)

因此,如果将LogEvent类型定义的dispatch_block_t block参数替换为RunTask:

So, if the dispatch_block_t block parameter of the LogEvent type definition was replaced by RunTask:

 void (^LogEvent)(..., RunTask runTask);

然后,Xcode将像这样自动完成LogEvent:

Then, Xcode would auto-complete LogEvent like this:

AppServices.logEvent(..., ^{ ...task B... });

顺便说一句,您将如何做到这一点,以便使用LogEvent的原始dispatch_block_t参数,自动完成功能将与然后阻塞参数(例如dispatch_async)的功能相同:

By the way, how would you make it so that, with the original dispatch_block_t parameter of LogEvent, auto-complete would do the same thing as then block parameter of, say, dispatch_async:

dispatch_async(<#dispatch_queue_t  _Nonnull queue#>, <#^(void)block#>)

当您键入dispatch_async时,Xcode为两个必需参数提供占位符.如果您两次单击选项卡,则block(最后一个)参数将以蓝色突出显示;否则,该参数将以蓝色突出显示.如果按回车键,则会发生这种情况:

When you type dispatch_async, Xcode provides placeholders for the two required parameters. If you hit tab twice, the block (last) parameter is highlighted in blue; if you press Return, this happens:

dispatch_async(<#dispatch_queue_t  _Nonnull queue#>, ^{
        <#code#>
    })

该块语句占位符变成一个块语句,该语句在该块内部具有一个代码占位符.我想知道该怎么做...

The block-statement placeholder turns into a block statement with a code placeholder inside the block. I want to know how to do that...

推荐答案

以下代码向您展示了如何在一个块内设置blocks,以便当您使用Xcode时,Xcode将自动完成参数并返回值阻止:

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:

在头文件中:

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;

在实施文件中:

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

在任何授予Task属性访问权限的类的实现文件中,键入属性的路径以及属性名称本身,直到Xcode自动完成其余部分为止:

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#>)

按Tab键前进到第一个参数(condition),然后按Return键.重复其余两个参数(successfailure):

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#>
    })

用您的代码替换code,确保为任何返回非空类型的块返回适当的值)(condition返回BOOL):

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");
    });

在此示例中,Task块根据Condition块中指定的条件的返回值执行SuccessFailure块.

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.

这篇关于Xcode自动完成一个块内的块(以及它们所在的块...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆