Swift 结构不符合协议 Equatable?
Swift Struct doesn't conform to protocol 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
}
我的结构在 class 中,所以它不起作用..我将这个结构从我的 class 中移出,现在它很好:)
Class 和 struct 不同。结构是值类型,但class是引用类型。
您不能在 class 中定义结构。相反,你不能在 struct.
中定义 class
Struct 和 class 都可以符合任何协议,包括您的自定义协议。
问题不在于该结构在 class 中。这当然是允许的,并且在很多情况下您可能想要这样做。问题在于 Equatable 协议的实施。您必须给出 == 的全局实现(您已经完成),但是没有实体 MyStruct....它是 ParentClass.MyStruct(如果该结构是在父 class 中定义的)。在这种情况下,下面的示例本身可能不是一个很好的示例,但它确实展示了如何在需要时执行此操作。
class ParentClass {
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 ParentClass.MyStruct: Equatable {}
func ==(lhs: ParentClass.MyStruct, rhs: ParentClass.MyStruct) -> Bool {
let areEqual = lhs.id == rhs.id &&
lhs.value == rhs.value
return areEqual
}
let s1 = ParentClass.MyStruct(id: 1, value: "one")
let s2 = ParentClass.MyStruct(id: 2, value: "two")
let s3 = ParentClass.MyStruct(id: 1, value: "one")
s1.description //blablabla
s1 == s2 //false
s3 == s1 //true
注意:我喜欢实现 Comparable 而不仅仅是 Equatable,这将使您能够支持排序和其他功能。
Swift 4.1(及以上)更新答案:
从Swift 4.1开始,您只需符合Equatable
protocol without the need of implementing the ==
method. See: SE-0185 - Synthesizing Equatable and Hashable conformance.
示例:
struct MyStruct: Equatable {
var id: Int
var value: String
}
let obj1 = MyStruct(id: 101, value: "object")
let obj2 = MyStruct(id: 101, value: "object")
obj1 == obj2 // true
请记住 ==
的默认行为是比较 所有 类型属性(基于示例:lhs.id == rhs.id && lhs.value == rhs.value
).如果你的目标是实现自定义行为(例如只比较一个属性),你必须自己做:
struct MyStruct: Equatable {
var id: Int
var value: String
}
extension MyStruct {
static func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
return lhs.id == rhs.id
}
}
let obj1 = MyStruct(id: 101, value: "obj1")
let obj2 = MyStruct(id: 101, value: "obj2")
obj1 == obj2 // true
此时,相等性将基于 id
值,而不管 value
的值是多少。
如何使结构符合协议 "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
}
我的结构在 class 中,所以它不起作用..我将这个结构从我的 class 中移出,现在它很好:)
Class 和 struct 不同。结构是值类型,但class是引用类型。
您不能在 class 中定义结构。相反,你不能在 struct.
中定义 classStruct 和 class 都可以符合任何协议,包括您的自定义协议。
问题不在于该结构在 class 中。这当然是允许的,并且在很多情况下您可能想要这样做。问题在于 Equatable 协议的实施。您必须给出 == 的全局实现(您已经完成),但是没有实体 MyStruct....它是 ParentClass.MyStruct(如果该结构是在父 class 中定义的)。在这种情况下,下面的示例本身可能不是一个很好的示例,但它确实展示了如何在需要时执行此操作。
class ParentClass {
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 ParentClass.MyStruct: Equatable {}
func ==(lhs: ParentClass.MyStruct, rhs: ParentClass.MyStruct) -> Bool {
let areEqual = lhs.id == rhs.id &&
lhs.value == rhs.value
return areEqual
}
let s1 = ParentClass.MyStruct(id: 1, value: "one")
let s2 = ParentClass.MyStruct(id: 2, value: "two")
let s3 = ParentClass.MyStruct(id: 1, value: "one")
s1.description //blablabla
s1 == s2 //false
s3 == s1 //true
注意:我喜欢实现 Comparable 而不仅仅是 Equatable,这将使您能够支持排序和其他功能。
Swift 4.1(及以上)更新答案:
从Swift 4.1开始,您只需符合Equatable
protocol without the need of implementing the ==
method. See: SE-0185 - Synthesizing Equatable and Hashable conformance.
示例:
struct MyStruct: Equatable {
var id: Int
var value: String
}
let obj1 = MyStruct(id: 101, value: "object")
let obj2 = MyStruct(id: 101, value: "object")
obj1 == obj2 // true
请记住 ==
的默认行为是比较 所有 类型属性(基于示例:lhs.id == rhs.id && lhs.value == rhs.value
).如果你的目标是实现自定义行为(例如只比较一个属性),你必须自己做:
struct MyStruct: Equatable {
var id: Int
var value: String
}
extension MyStruct {
static func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
return lhs.id == rhs.id
}
}
let obj1 = MyStruct(id: 101, value: "obj1")
let obj2 = MyStruct(id: 101, value: "obj2")
obj1 == obj2 // true
此时,相等性将基于 id
值,而不管 value
的值是多少。