Non-Nominal Types cannot be filtered. This is because they are not equatable:
i.e. Here are some Non-Nominal Types
What you could do is create a struct like so:
struct Method: Hashable, Equatable {
var name: String
var method: () -> () // or whatever type you want
init(_ n: String,_ m: @escaping () -> ()) {
name = n
method = m
}
// Equatable
static func == (lhs: Method, rhs: Method) -> Bool {
return lhs.name == rhs.name
}
// Hashable
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}
So that the name is what checks if methods are equal. But so much for structs.
If you wanted, you could make a similar class instead to keep track of the reference.
Example code:
func function1() {print("foo")}
func function2() {print("bar")}
var array: [Method] = []
array.append(Method("One", function1))
array.append(Method("Two", function2))
let function: Method
function = Method("One", function1)
var filteredArray: [Method] = []
filteredArray = array.filter { $0 != function}
print(filteredArray.count) // 1
filteredArray.first?.method() // prints "bar"
()->()it is notEquatable$0()will execute the function code and returnVoidtherefore all elements are equal tofunctionwhich isVoidthats why it return an empty array.