Objective-C中块和参数之间的差异

时间:2015-08-31 22:09:01

标签: objective-c arguments objective-c-blocks

在Objective C中,如果一个块可以接受参数并返回值(https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html),

那么块和参数之间的区别是什么?

1 个答案:

答案 0 :(得分:3)

一个块只是代码的一部分,就是全部。

另一方面,参数或参数是传递给函数或块的值,因此该块中的代码可以使用它。

例如:

^(int anIntegerArgument){
    //this is inside of a block
}

这整个陈述被称为一个块。但是,值anIntegerArgument是一个参数。

如果您需要将一些自定义代码传递给方法,则块可以成为参数。例如,在运行动作后的SpriteKit中,您可以选择在完成后运行一些代码。为此,您传入一个块作为参数:

[self runAction:anAction completion:^{
     //block run at the end of an action
}];