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)
source
share