Swift - 匿名闭包参数不包含在闭包中

时间:2014-08-22 22:44:30

标签: ios objective-c iphone swift

我正在通过教程学习Swift语言。

我正在使用此代码:

let hasPrefixAndSuffix: (String,String,String) -> Bool
{
  var hasPrefix = $0.hasPrefix($1)
  var hasSuffix = $0.hasSuffix($2)
  return hasPrefix && hasSuffix
}

我发现了这个错误:

Anonymous closure argument not contained in a closure.

1 个答案:

答案 0 :(得分:1)

您忘记了=实际上将该闭包分配给变量。

let hasPrefixAndSuffix: (String,String,String) -> Bool = {
  var hasPrefix = $0.hasPrefix($1)
  var hasSuffix = $0.hasSuffix($2)
  return hasPrefix && hasSuffix
}