0

I'm trying to remove an object from an Array and I'm using the filter function:

protocol OnLogListener {
    func onLog(log: Log)
}

class Logger {

    static private var listeners = [OnLogListener]()

    static func removeOnLogListener(listener: OnLogListener) {
        listeners = listeners.filter({ $0 !== listener })
    }

}

The compiler is complaining with the following error:

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

I don't understand the error, the closure is returning a Boolean and using the OnLogListener parameter it is a (OnLogListener) -> Boolean closure.

eliocs
  • 18,511
  • 7
  • 40
  • 52
  • 1
    What is `OnLogListener`? If it is a *protocol* then this could help: http://stackoverflow.com/questions/30344222/find-index-of-object-in-an-array-of-type-someprotocol. – Martin R Jul 03 '15 at 09:17
  • I'm new to Swift but I imagined that *protocols* always applied to classes – eliocs Jul 03 '15 at 09:24
  • No, both structs and classes can conform to a protocol. If the protocol should be restricted to classes then it must be declared as a "class protocol": `protocol OnLogListener : class {... }` as explained in the answer to the other question. – Martin R Jul 03 '15 at 09:27
  • Yep I solve the issue, I think it is worth for other newcommers for you to post the answer so I can mark the question as solved – eliocs Jul 03 '15 at 09:28
  • To be more precise, enumerations count in as well, so, classes, structs and enumerations can adopt protocol. https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html – zrzka Jul 03 '15 at 09:28
  • Glad to help. But since it is the same problem with the same solution, I prefer to close this as a duplicate. – Martin R Jul 03 '15 at 09:30

0 Answers0