Swift Struct doesn't conform to protocol Equatable?

前端 未结 4 2033
轻奢々
轻奢々 2021-02-07 01:37

How do I make a structure conform to protocol \"Equatable\"?

I\'m using Xcode 7.3.1

struct MyStruct {
   var id: Int
   var value: String

   init(id: In         


        
4条回答
  •  迷失自我
    2021-02-07 02:04

    OK, after lots of searching, it's working...

    struct MyStruct {
        var id: Int
        var value: String
    
        init(id: Int, value: String) {
            self.id = id
            self.value = value
        }
    
        var description: String {
            return "blablabla"
        }
    
    }
    
    extension MyStruct: Equatable {}
    
    func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
        let areEqual = lhs.id == rhs.id &&
            lhs.value == rhs.value
    
        return areEqual
    }
    

    My Struct was in a class, so it didn't work.. I moved this Struct out of my class and now it's good :)

提交回复
热议问题