1

i want to check equality between objects but i get an error. Error is within the code.

class Test {
    var key: String

    init(nameParam: String) {
        self.key = nameParam
    }


    func ==(other: Test) -> Bool {
        return other.key == self.key
    }

}

var t1 = Test(nameParam: "Test")
var t2 = Test(nameParam: "Test1")

if(t1 == t2) { // Error: 'Test' is not convertible to 'MirrorDisposition'
    println("...")
}
JKhlr
  • 21
  • 1
  • possible duplicate of [Swift Equatable Protocol](http://stackoverflow.com/questions/24467960/swift-equatable-protocol) – Martin R Nov 19 '14 at 10:52

2 Answers2

3

Operators must be implemented in the global scope, not inside the class. So you should implement your equality operator outside of the class:

class Test {...}

func == (lhs: Test, rhs: Test) -> Bool {
    return lhs.key == rhs.key
}

Suggested reading: Operator Functions

Antonio
  • 71,651
  • 11
  • 148
  • 165
1

You should write it like that:

class Test : Equatable {
    var key: String

    init(nameParam: String) {
        self.key = nameParam
    }




}

func ==(lhs:Test,rhs: Test) -> Bool {
    return lhs.key == rhs.key
}

var t1 = Test(nameParam: "Test")
var t2 = Test(nameParam: "Test1")

if(t1 == t2) {
    println("...")
}