Abdou023 Abdou023 -5 years ago 113
Swift Question

Errors when working with protocols as if they were classes

I have a protocol like this:

protocol Moveable {
var moveSpeed: Float { get set }

And in one of my classes that don't conform to "Moveable" I have a function like this:

var moveables: [Moveables]()
func checkMoveable(mov: Moveable) -> Bool {
for m in moveables {
if m === mov { // <- Error
return true
return false

I get this error:

Binary operator '===' cannot be applied to two 'Moveable' operands

also tried with "==" instead of "==="

And in another function that relies on the previous one:

func removeMoveable(mov: Moveable) {
if checkMoveable(mov) {
self.moveables = moveables.filter({$0 !== mov})// <- Error

I get this error:

Cannot convert value of type 'Moveable' to expected argument type

If "Moveable" was a class instead of being a protocol none of these errors would appear.

Answer Source

The identity operator === applies only to reference types (classes). Your protocol can be adopted by either reference or value types, so something whose only type information is that protocol can't be used with the identity operator.

If you want your protocol to be adopted only by classes, declare it thusly:

protocol Movable: class { //...

Or make it extend a protocol that's already restricted to classes:

protocol Movable: NSObjectProtocol { //...

The equivalence operator == applies only to types that are Equatable. If you want to use that operator on two Movables, you'll need to declare that for a type to be Movable it must also conform to Equatable:

protocol Movable: Equatable { //...

Since it seems you're looking for a particular SKSpriteNode instance, the first (making === work) is probably what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download