我正在通过教程学习Swift lang。

我正在使用此代码:

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.

最佳答案

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

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

关于ios - Swift-闭包中不包含匿名闭包参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25456567/

10-10 23:38