Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift contains extension for Array

I'm trying to add an extension method in Array like so:

extension Array {
    func contains(obj: T) -> Bool {
        let filtered = self.filter {$0 == obj}
        return filtered.count > 0
    }
}

But self.filter {$0 == obj} don't work. Compiler error:

could not find an overload for '==' that accepts the supplied arguments

like image 655
Gralex Avatar asked Nov 26 '22 21:11

Gralex


1 Answers

you don't actually need to write an extension, you can use the global func contains from the Swift library:

contains([1,2,3], 1)
like image 52
atermenji Avatar answered Dec 30 '22 07:12

atermenji