微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift Struct不符合协议Equatable?

如何使结构符合协议“Equatable”?

我正在使用Xcode 7.3.1

struct MyStruct {
   var id: Int
   var value: String

   init(id: Int,value: String) {
       self.id = id
       self.value = value
   }

   var description: String {
       return "blablabla"
   }

}

当我使用“MyStruct”时,Xcode显示错误

MyStruct does not conform to protocol “Equatable”

你有想法使MyStruct符合协议吗?

好的,经过大量的搜索,它正在工作……
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
}

我的结构是在一个类中,所以它没有用.我把这个Struct移出了我的班级,现在它很好:)

原文地址:https://www.jb51.cc/swift/319605.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐