Swift Equatable on a protocol

前端 未结 10 2145
时光说笑
时光说笑 2021-01-30 12:42

I don\'t think this can be done but I\'ll ask anyway. I have a protocol:

protocol X {}

And a class:

class Y:X {}
10条回答
  •  星月不相逢
    2021-01-30 13:29

    maybe this will be helpful for you:

    protocol X:Equatable {
        var name: String {get set}
    
    }
    
    extension X {
        static func ==(lhs: Self, rhs: Self) -> Bool {
            return lhs.name == rhs.name
        }
    }
    
    struct Test : X {
        var name: String
    }
    
    let first = Test(name: "Test1")
    let second = Test(name: "Test2")
    
    print(first == second) // false
    

提交回复
热议问题