Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: binary operator '==' cannot be applied to operands of type "protocol"

I have a protocol:

protocol ProfileManagerDelegete {
func dataHaveUpdated(type: ReturnType)

}

and create a protocol array, and add/remove listener:

var listeners: [ProfileManagerDelegete] = []

func addListener(listener: ProfileManagerDelegete) {
    listeners.append(listener)
}

func removeLister(listener: ProfileManagerDelegete) {
    for lis in listeners {
        if lis == listener { // this line error
            //remove listener
        }
    }
}

Anyone can help ?

like image 984
Cindy Avatar asked Dec 17 '15 02:12

Cindy


1 Answers

Because you have not told Swift how to compare 2 objects of type ProfileManagerDelegete. Define a function:

protocol ProfileManagerDelegete {
    func dataHaveUpdated(type: ReturnType)
}

func == (lhs: ProfileManagerDelegete, rhs: ProfileManagerDelegete) -> Bool {
    // decide if they are equal
}
like image 169
Code Different Avatar answered Sep 28 '22 06:09

Code Different