Swift Equatable Protocol
Asked Answered
U

3

85

I was folling this tutorial for Swift: https://www.raywenderlich.com/125311/make-game-like-candy-crush-spritekit-swift-part-1 and came across this code:

func == (lhs: Cookie, rhs: Cookie) -> Bool {
    return lhs.column == rhs.column && lhs.row == rhs.row
}

I wrote exactly that, but Xcode is giving my these errors:

Consecutive declarations on a line must be separated by ';'
Expected declaration operators are only allowed at global scope

I found this code from apple's documentation: https://developer.apple.com/documentation/swift/equatable

Which is very similar to what I wrote. Whats wrong? This seems like a bug to me. I am using Xcode 6 Beta 2

EDIT:

This is my whole Cookie class:

class Cookie: Printable, Hashable {
    var column: Int
    var row: Int
    let cookieType: CookieType
    let sprite: SKSpriteNode?
    
    init(column: Int, row: Int, cookieType: CookieType) {
        self.column = column
        self.row = row
        self.cookieType = cookieType
    }
    
    var description: String {
        return "type:\(cookieType) square:(\(column),\(row))"
    }
    
    var hashValue: Int {
        return row * 10 + column
    }
    
    func ==(lhs: Cookie, rhs: Cookie) -> Bool {
        return lhs.column == rhs.column && lhs.row == rhs.row
    }
}
Unhandy answered 28/6, 2014 at 14:47 Comment(5)
What's the code before that statement? It works fine for me by itselfAnnmarie
I added the whole class to the descriptionUnhandy
"declaration Operators are only allowed at global scope" Pretty darned clear. This is one of Swift's better compiler error messages!Hawkie
you can overload an operator in the file-scope only.Jequirity
You need to move func ==(lhs: Cookie, rhs: Cookie) -> Bool {...} OUT of the Cookie class!!Hiero
A
145

Move this function

func == (lhs: Cookie, rhs: Cookie) -> Bool {
    return lhs.column == rhs.column && lhs.row == rhs.row
}

Outside of the cookie class. It makes sense this way since it's overriding the == operator at the global scope when it is used on two Cookies.

Annmarie answered 28/6, 2014 at 14:58 Comment(3)
I would like to add that on xCode 6.3.2 and swfit 1.2, func == must be immediately after the class or struct definition. Even adding a simple sentence like "var a = 1" will bring back the compiler error.Shedd
I would've never thought to put it outside the class! What's that even called? How do I find it on google?Hypoderma
There's an explanation about why the operator overload is in the global scope, although a possible change is being discussed to let the operator implementation be inside the type.Mirandamire
D
32

SWIFT 2:

As in swift 2 NSObject already conforms to Equatable.You don't need conformance at the top so it's like

class Cookie: NSObject {
    ...

}

And you need to override isEqual method as

class Cookie:NSObject{
    var column: Int
    var row: Int

    //..........

    override func isEqual(object: AnyObject?) -> Bool {
        guard let rhs = object as? Cookie else {
            return false
        }
        let lhs = self

        return lhs.column == rhs.column
    }

}

This time isEqual method is inside the class. :)

EDIT for SWIFT 3: Change this method as

override func isEqual(_ object: AnyObject?) -> Bool {
        guard let rhs = object as? Cookie else {
            return false
        }
        let lhs = self

        return lhs.column == rhs.column
    }
Deloris answered 29/12, 2015 at 16:42 Comment(0)
D
6

making the class an NSObject solved the equatable problems for me...

class Cookie: NSObject {
...
}

(got the tip from the iOS apprentice tutorials)

Delainedelainey answered 6/9, 2015 at 12:58 Comment(1)
That would be because NSObject implements the following on line 70 of the NSObject swiftDoc extension NSObject : Equatable, Hashable.Talie

© 2022 - 2024 — McMap. All rights reserved.