块声明语法列表 [英] Block Declaration Syntax List
问题描述
众所周知,目标C(甚至我认为确实是C)中的块语法是不一致的.将块作为参数传递看起来与将块声明为ivars有所不同,这看起来与typedef
块不同.
Block syntax in Objective C (and indeed C, I presume) is notoriously incongruous. Passing blocks as arguments looks different than declaring blocks as ivars, which looks different than typedef
ing blocks.
是否有可供我快速参考的块声明语法的完整列表?
Is there a comprehensive list of block-declaration syntax that I could keep on hand for quick reference?
推荐答案
块声明语法列表
穿越,让我们
List of Block Declaration Syntaxes
Throughout, let
-
return_type
是对象/原始/等的类型.您想返回(通常为void
) -
blockName
是要创建的块的变量名 -
var_type
是类型object/primitive/etc.您想作为参数传递(不带任何参数保留空白) -
varName
是给定参数的变量名
return_type
be the type of object/primitive/etc. you'd like to return (commonlyvoid
)blockName
be the variable name of the block you're creatingvar_type
be the type object/primitive/etc. you'd like to pass as an argument (leave blank for no parameters)varName
be the variable name of the given parameter
请记住,您可以创建任意数量的参数.
And remember that you can create as many parameters as you'd like.
可能是最常见的声明方式.
Possibly the most common for of declaration.
return_type (^blockName)(var_type) = ^return_type (var_type varName)
{
// ...
};
块作为属性
就像将块声明为变量一样,但是有细微的差别.
Blocks as Properties
Much like declaring blocks as variables, however subtly different.
@property (copy) return_type (^blockName) (var_type);
将块作为参数
请注意,这与块作为参数"不同;在这种情况下,您要声明一个需要块参数的方法.
Blocks as Parameters
Note that this is distinct from "Blocks as Arguments"; in this instance, you're declaring a method that wants a block argument.
- (void)yourMethod:(return_type (^)(var_type))blockName;
将块作为参数
请注意,这与块作为参数"不同;在这种情况下,您正在调用一个需要带有匿名块的块参数的方法.如果已经声明了块变量,则只需将变量名作为参数传递即可.
Blocks as Arguments
Note that this is distinct from "Blocks as Parameters"; in this instance, you're calling a method that wants a block argument with an anonymous block. If you have already declared a block variable, it is sufficient to pass the variable name as the argument.
[someObject doSomethingWithBlock: ^return_type (var_type varName)
{
//...
}];
匿名块
从功能上讲,这是一个匿名块,但是将块分配给变量的语法只是将变量设置为等于一个匿名块.
Anonymous Block
This is functionally an anonymous block, however the syntax for assigning blocks to variables is simply to set the variable equal to an anonymous block.
^return_type (var_type varName)
{
//...
};
typedef
块
这使您可以设置一个短名称,该短名称可以在块声明期间像其他任何类名称一样被引用.
typedef
Block
This allows you to set up a short name that can be referenced just like any other class name during the declaration of blocks.
typedef return_type (^blockName)(var_type);
要在以后使用blockName
而不是标准块声明语法,只需替换即可.
To then later use blockName
instead of the standard block declaration syntax, simply substitute.
可以说这是对块的不太有用的利用,但是可能仍然占有一席之地.内联块是实例化后立即调用的匿名块.
This is arguably a less useful utilization of blocks, but may have its place nonetheless. An inline block is an anonymous block called immediately after instantiation.
^return_type (var_type varName)
{
//...
}(var);
内联块主要用于范围偏移,并且大致等效于用大括号分隔的简单代码段.
Inline blocks are primarily useful for scope offsetting, and are roughly equivalent to simple brace-delimited chunks of code.
{
//...
}
递归块
这允许您从自身调用一个块,从而创建一个可在回调和GCD调用期间使用的循环.这种实例化方法没有ARC中的保留周期.
Recursive Blocks
This allows you to call a block from itself, creating a loop that can be used during callbacks and GCD calls. This instantiation method is free of retain cycles in ARC.
__block return_type (^blockName)(var_type) = [^return_type (var_type varName)
{
if (returnCondition)
{
blockName = nil;
return;
}
// ...
} copy];
blockName(varValue);
返回块
方法可以返回一个块,
Returning Blocks
A method can return a block,
- (return_type(^)(var_type))methodName
{
// ...
}
一个函数也可以,如果有些奇怪的话.
as can a function, if a bit strangely.
return_type (^FunctionName())(var_type)
{
// ...
}
附录
如果我错过了任何事情,请在评论中让我知道,我将进行研究/添加.
Addendums
If I've missed anything, please let me know in comments, and I'll research/add them.
blockName = (varName: var_type) -> (return_type)
这几乎是一种语言功能.
It's almost like it's a language feature.
这篇关于块声明语法列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!