Reputation: 103
I'm trying to extend the array class writing a simple function that will receive a function as parameter and will use that to filter the current array.
In this example "self" is actually an array.
this is what I used so far:
func applyFilter(filterFunction: (String) -> Bool) {
self.filter(filterFunction)
}
But I'm receiving this error from xCode:
Any ideas?
Upvotes: 0
Views: 540
Reputation: 306
The error message means that, the generic Array.filter(_:)
function is expecting the Array.Element
to be passed not a string.
If you change the extension and function to be:
extension Array where Element == String {
func applyFilter(_ filter: (String) -> Bool) {
self.filter(filter)
}
}
As this specifies that the function will only be dealing with String elements then you can pass a function to the closure as such:
func filter(_ value: String) -> Bool {
/* Filter how you need */
}
["Hello", "World"].applyFilter(filter(_:))
Upvotes: 3