带返回类型的内联块 [英] Inline Block With Return Type

查看:71
本文介绍了带返回类型的内联块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中可以创建一个内联块并使用其返回类型吗?例如,是否可以创建一个返回BOOL的块,使其内联,并将其返回类型用于赋值.

Is it possible in Objective-C to create a block inline and use its return type? For example, can I create a block that returns a BOOL, have it be inline, and use its return type for an assignment.

BOOL b = <inline block that returns BOOL> { // unsure of the syntax / legality of this
    return YES; // will be more logic in here, that is why I want to use a block.
};

我在使用块语法时遇到麻烦,并且不确定是否可以内联创建块.我检查了以下资源,无济于事.

I am having trouble with the block syntax, and am unsure if a block can be created inline. I have checked the following resources with no avail.

  • Block Cheat Sheet
  • Gosh Darn Block Syntax
  • And of Course, The Apple Documentation

如果这是不可能的,或者非常容易,那么谢谢您的时间和耐心.

Thankyou for your time and patience if this happens to be impossible, or very easy.

推荐答案

您需要使用以下内容:

BOOL b = ^(){ return YES; }();

这实际上创建了块,然后调用它.虽然不好看,所以您也可以做这样的事情:

This actually creates the block and then calls it. It's not nice to read though, so you may as well just do something like this:

BOOL b = NO;
{
    // Other stuff, will be local
    b = YES;
}

这篇关于带返回类型的内联块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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