Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Conforming to protocol: Equatable

I've been working on a chess game where I defined a struct to represent the coordinate system:

struct Point {
    var x: Int
    var y: Int
}

In certain points of the app, I want to compare two Point values to see if they match or not. For example:

let pointOne = Point(x: 2, y: 2)

let pointTwo = Point(x: 3, y: 3)

if pointOne == pointTwo {

print("Points are equal")

}

But this yields an error: "Binary operator '==' cannot be applied to two 'Point' operands". I guess the Point type must conform to the Equatable protocol such as:

struct Point: Equatable {

var x: Int

 var y: Int

}

But this also yields an error that Point won't conform to Equatable as it is now. I guess I must add certain code inside the struct Point so that it will conform to Equatable. But I couldn't find related information of what to do after I checked the documentation and Googled it.

Hence, I would appreciate if you can help me out with this.

Thanks in advance,

Mert

2 Answers

Hello:

By conforming to the protocol equatable, you have to implement the "equatable" code.

Here is an extension to your Point struct. This will help you.

struct Point {
    var x: Int
    var y: Int
}


// You conform to the equatable protocol by 
// implementing the "==" function, which takes 2 arguments 
// of your struct, and compares them. 

extension Point: Equatable {

    static func == (lhs: Point, rhs: Point ) -> Bool {    

        return
            lhs.x == rhs.x &&
            lhs.y == rhs.y
    }
}


var somePoint = Point(x: 2, y: 2)
var anotherPoint = Point(x: 3, y: 2)

if somePoint == anotherPoint {
    print("True")
} else {
    print("False")  // Prints false
}

Hope this helps with your project. Good luck

Hi Mert,

You should look to the documentation when you are attempting to conform to a Swift protocol like Equatable. You can do this right from XCode, by command (or is it option?) - clicking on the protocol's name.

You typically can't just say you conform to a protocol: you have to actually conform to it! Equatable is a simple protocol: it requires you to implement the operator ==. Again, you can learn this by reading the docs.

Here's how I would do it for point: [Edited - thanks Jhoan Arango!]

struct Point: Equatable {
    var x: Int
    var y: Int

    static func ==(lhs: Point, rhs: Point) -> Bool {
        return lhs.x == rhs.x && lhs.y == rhs.y
    }

}

That might look a little scary, but essentially, we're just saying "If the left and right hand sides of == are Points, == returns true if the left Point's x value matches the right Point's x value and the left Point's y value matches the right Point's y value.

Let me know if that makes sense!

Cheers :beers:

-Greg

P.S. Here's a working example.

Hey Greg,

Question ? why are you using generics in this example .. ? I'm a bit lost there

No reason haha. I was copy/pasting from somewhere, and didn't remove them. Also, forgot to add static. Thanks!

Oh ok got it..

Yeah I was writing this comment when I saw you corrected it:

"In fact when testing this version it gives me an error that says: "Can not specialize non-genetic type Point". Also, in order to fully conform to the "equatable" protocol, the function has to be "static".

   static func == (lhs: Point, rhs: Point) -> Bool {
        return lhs.x == rhs.x && lhs.y == rhs.y
    }

"

Good answer :)