3

I have the following protocol defined in Swift:

protocol RecordingObserver {
    func aFunc()
}

Somewhere I have to compare two objects that implement this protocol, to check if they are the same. The problem I'm facing is that apparently Swift doesn't allow us to do this:

func areEqual(a:RecordingObserver,b:RecordingObserver){
    if a === b {
        println("Equal")
    }
}

Any idea why this is happening? And how I can do this in another way?

6
  • You've posted some code; could you also tell us the errors/unexpected behavior you've experienced? What is the this in Why is this happening? Commented Apr 29, 2015 at 11:45
  • @waldrumpus His a === b isn't returning the correct value (that they are equal). Commented Apr 29, 2015 at 11:51
  • @kmcgrady Possibly, but I think this question could become really nice with a bit more information by the asker. Commented Apr 29, 2015 at 11:59
  • @waldrumpus I guess you're right. While it is quite easy to presume what the poster is asking it would be better if it was crystal clear. Commented Apr 29, 2015 at 12:00
  • You need to be clearer about what you want to do. Are you trying to see if a and b both contain the exact same object (not objects with the same values, but the very same instance of an object?) If that's what you want, then why not use the === operator directly? That's what it's for. If not, you need to make the RecordingObserver protocol also conform to the Equitable protocol. Then for all objects that conform to RecordingObserver, you'll need to implement the == operator. Commented Apr 29, 2015 at 12:50

4 Answers 4

5

=== is the identical to operator and is used to test whether two object references both refer to the same object instance. It can be applied only to reference types (i.e. instances of a class).

=== is different from the "equal to" operator == (which is required in the Equatable protocol).

Therefore, assuming that

  • the actual observers are instances of a class, and
  • your intention is to check if a and b refer to the same instance,

you have to define the protocol as a class protocol:

protocol RecordingObserver : class {
    // ...
}

Then

func areEqual(a:RecordingObserver,b:RecordingObserver){
    if a === b {
        println("a and b refer to the same object instance")
    }
}

compiles (and works as expected) because the compiler knows that a and b are reference types.

Sign up to request clarification or add additional context in comments.

Comments

3

Your class needs to support the Equatable protocol to use ==

https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Equatable.html

Or if you want to use === something like this...

protocol RecordingObserver {
    func aFunc()
}

class MyClass: RecordingObserver {
    func aFunc() {
        // Do something
    }
}

func areEqual(a: MyClass, b: MyClass){
    if a === b {
        println("Equal")
    }
}

1 Comment

Equatable ensures that the type implements ==, not ===, so that would not solve the compiler error. (Of course we don't know yet if === is what OP really wants.)
1

I believe there is an 'isEqual' method on NSObject. If your custom objects are both subclassed from that you should be able to compare a.isEqual(b).

Comments

0

It is because you said that you objects implement only RecordingObserver. So compiler don't know if he can compare them.

Try this:

func areEqual<T where T: Equatable, T: RecordingObserver>(a: T,b: T) {

}

You can just copy this code into single view project to test:

protocol RecordingObserver {

}

class SomeClass: NSObject, RecordingObserver {
}

class ViewController: UIViewController {

    func areEqual<T where T: Equatable, T: RecordingObserver>(a: T,b: T) -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let a = SomeClass()
        let b = SomeClass()

        NSLog("\(areEqual(a, b: b))")
    }

}

1 Comment

That won't work either unless the RecordingObserver protocol conforms to the Equitable protocol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.