swift中闭包参数的语法是什么?

joe*_*els 5 closures swift

在Swift头文件中,isSeparator:参数接受一个闭包

public func split(maxSplit: Int = default, allowEmptySlices: Bool = default, @noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]
Run Code Online (Sandbox Code Playgroud)

但是在文档中,它以不同的方式列出了闭包语法

{ (parameters) -> return type in
    statements
}
Run Code Online (Sandbox Code Playgroud)

你怎么知道(Self.Generator.Element) throws -> Bool rethrows引用一个闭包/需要一个闭包?是否还有其他方式,标题/文档可能会将参数列为闭包?

luk*_*302 7

赠送这个是封闭的"东西"是->.完整的类型是

(Self.Generator.Element) throws -> Bool
Run Code Online (Sandbox Code Playgroud)

这意味着闭包采用类型的变量,Self.Generator.Element并且必须Bool基于输入返回一些计算.这样做可能会产生一些错误 - 这就是投掷的目的.

你写的是什么

{ (parameters) -> return type in
    statements
}
Run Code Online (Sandbox Code Playgroud)

将是一个实际的实现,一些通用闭包类型的值.

闭包的类型例如是(someInt:Int, someDouble:Double) -> String:

var a : ((someInt:Int, someDouble:Double) -> String)
Run Code Online (Sandbox Code Playgroud)

另外,赠送a实际上是一个闭包的东西是->在类型声明中.

然后你a通过第二个代码块之后的一些代码片段分配一些内容:

a = { (integer, floating) -> String in
    return "\(integer) \(floating)"
}
Run Code Online (Sandbox Code Playgroud)