Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke 'filter' with an argument list of type '((_) -> _)'

Tags:

swift

Sounds ridiculous, but I'm unable to fix this piece of code:

self.runningScripts.filter({ $0 != scriptRunner })

No matter how I write the closure I always get this error:

Cannot invoke 'filter' with an argument list of type '((_) -> _)'

runningScripts is defined like this:

var runningScripts = [ScriptRunner]()

and ScriptRunner is a Swift class (does not inherit from NSObject)

I'm using nearly the same in many other places without problems. Any suggestions?

like image 659
idmean Avatar asked Apr 29 '15 14:04

idmean


2 Answers

You can get that error if you didn't make ScriptRunner conform to Equatable:

class ScriptRunner : Equatable {
    // the rest of your implementation here
}

func ==(lhs: ScriptRunner, rhs: ScriptRunner) -> Bool {
    return ... // change this to whatever test that satisfies that lhs and rhs are equal
}
like image 82
Rob Avatar answered Nov 13 '22 01:11

Rob


I needed an explicit cast like this:

@NSManaged private var storage: [String]
    private var  objects: Set<String>?
    func remove(element:String) {
        initSetIfNeeded()
        if(objects!.contains(element)) {
            objects!.remove(element)
            storage = storage.filter({($0 as NSObject) !== (element as NSObject)}) // Explicit cast here!!
        }
    }
like image 1
Jitendra Kulkarni Avatar answered Nov 13 '22 01:11

Jitendra Kulkarni