I have a question, during study of closure.
I want make datatypes closure method like in array types .sort(), .filter(), .reduce(), .map()
But how can I make this things. Its datatype not a class.
I want make
array.somemethod({closure})
not a
Somefunc(input: array, closure : { .... })
-
Can I make datatype method in swift?
otherwise, I can use func only?
You just need extend Array and pass a closure as your method argument. Lets say you would like to create a mutating method to work as the opposite of filter (to remove elements of your array based on a condition):
Another option extending
RangeReplaceableCollection
:Usage:
or using trailing closure syntax