【问题标题】:Swift : Cannot invoke 'filter' with an argument list of type '(@noescape (Int) throws -> Bool)'Swift:无法使用类型为“(@noescape(Int)抛出-> Bool)”的参数列表调用“过滤器”
【发布时间】:2015-11-02 16:40:37
【问题描述】:

我被这个错误困住了:

func compactCoords(coords: [Int]) -> [Int]{
    return coords.filter({ (value) -> Bool in
        return value != 0
    })
}

不能使用类型为“(@noescape (Int) throws -> Bool)”的参数列表调用“过滤器”

感谢您的帮助!

【问题讨论】:

  • 为了帮助你,我需要更多的上下文。你如何调用该函数?上次出现此错误时,我尝试将返回的过滤器函数数组应用于集合。因此,您可能会因为调用该函数的方式而收到错误...
  • 托尼史塔克谢谢。这对我有帮助。 :)

标签: swift


【解决方案1】:

您的代码在 Xcode 7.1 中运行良好。您可能不小心尝试在 Xcode 6.x 中运行此代码?

你可以像这样缩短你的函数:

func compactCoords(coords: [Int]) -> [Int] {
    return coords.filter { $0 != 0 }
}

输出:

let coords = [1,2,3,0,4,5,6]
let compactedCoords = compactCoords(coords) // [1, 2, 3, 4, 5, 6]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多