3

I'm developing first app using Swift. in one of my Class I need to store closure in an Array. Like an event manager :

typealias eventCallback = (eventName:String, data:[MasterModel]?) -> Void;

class MHttpLoader: NSObject
{
    var eventRegister : [String: [eventCallback]];

    class var instance : MHttpLoader
    {
        struct Static {
            static let instance : MHttpLoader = MHttpLoader(baseURL:NSURL(string:"http://192.168.0.11:3000"));
        }
        return Static.instance;
    }

    class func registerEvent(eventName:String, callback:eventCallback)
    {
        if var tab = MHttpLoader.instance.eventRegister[eventName]
        {
            tab.append(callback);
        }
        else
        {
            MHttpLoader.instance.eventRegister[eventName] = [callback];
        }
    }

    func fireEvent(eventName: String, data:[MasterModel]?)
    {
        if let tab = self.eventRegister[eventName]
        {
            for callback in tab
            {
                callback(eventName:eventName, data:data);
            }
        }
    }
}

All this code work pretty well, the problem is when i want to remove a closure from my array. For exemple :

class func removeEvent(eventName:String, callback:eventCallback)
{
    if var tab :Array = MHttpLoader.instance.eventRegister[eventName]
    {
        if let index = find(tab, callback) as? Int
        {
            tab.removeAtIndex(index);
        }
    }
}

I have the error which says that my closure is not conform to protocol "Equatable"

I also tried :

class func removeEvent(eventName:String, callback:eventCallback)
{
    if var tab :Array = MHttpLoader.instance.eventRegister[eventName]
    {
        tab = tab.filter({ (currentElement) -> Bool in
            return currentElement != callback;
        });
    }
}

But I have the error : Cannot invoke '!=' with an argument list of type '((eventCallback), eventCallback)'

Here is my question how can i find the index of closure in array or simply compare closure?

Thank you

Fogia
  • 127
  • 1
  • 8
  • possible duplicate of [How do you test functions and closures for equality?](http://stackoverflow.com/questions/24111984/how-do-you-test-functions-and-closures-for-equality) – rintaro Jan 29 '15 at 10:30

0 Answers0