Reputation: 29316
var arr = [4, 5, 23, 4, 5, 2, 3]
arr.filter({
return true
})
I get the error 'Int' is not a subtype of ()
. What am I doing wrong? Everything I've read seems to indicate this should work.
Upvotes: 0
Views: 134
Reputation: 13179
You have to provide an index to the filter:
arr.filter({
n in true
})
Upvotes: 0
Reputation: 534950
var arr = [4, 5, 23, 4, 5, 2, 3]
let arr2 = arr.filter {
_ in
return true
}
Problems with your original code:
filter
returns an array. If you don't capture it, nothing useful happens. Notice that I've captured it in another variable; you could also reassign to the original variable.
If a parameter is passed into an anonymous function, you must capture it. You can capture it namelessly, as here, or with a name, or as $0
, but you cannot completely ignore it; you must acknowledge it. That is what my _ in
does.
Upvotes: 2