Swift: Unable to invoke "filter" using argument list like (@noescape (Int) throws & # 8594; Bool) '

I am stuck with this error:

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

Cannot call 'filter' using argument list of type '(@noescape (Int) throws -> Bool)'

Thank you for your help!

+4
source share
1 answer

Your code works fine in Xcode 7.1. Perhaps you accidentally try to run this code in Xcode 6.x?

You can shorten your function as follows:

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

Output:

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

Source: https://habr.com/ru/post/1614172/


All Articles