更新:
从包含 Swift 4.1 的 Xcode 9.3 开始,数组相等性按预期工作,原始问题中的代码编译没有错误。
但是,请查看已接受的答案,因为它提供了更好、更现代的解决方案。
原题如下:
当我尝试声明类型为 [Post]
的通用枚举实例时,我收到一条错误消息
Type '[Post]' does not conform to protocol 'Equatable'
这是无稽之谈,因为 Post
符合 Equatable
我实际上可以比较两个 [Post]
没有编译错误的实例?
在下面的示例中,我扩展了 Post
和 Result<T>
类型为 Equatable
然后我做了一些测试:
- 测试我可以比较两个
Post
类型:OK - 测试我可以比较两个
[Post]
类型:OK - 测试我可以比较两个
Result<Post>
类型:OK - 测试我可以比较两个
Result<[Post]>
类型:错误
import Foundation
struct Post {
let text: String
}
extension Post: Equatable {}
func ==(lhs: Post, rhs: Post) -> Bool {
return lhs.text == rhs.text
}
enum Result<T: Equatable> {
case success(result: T)
case error
}
extension Result: Equatable {
static func ==(lhs: Result<T>, rhs: Result<T>) -> Bool {
switch (lhs, rhs) {
case let (.success(lhsVal), .success(rhsVal)):
return lhsVal == rhsVal
case (.error, .error):
return true
default:
return false
}
}
func test() {
// Test 1: Check Post type for equality: OK
let post1: Post = Post(text: "post 1")
let post2: Post = Post(text: "post 2")
if post1 == post2 {
print("equal posts")
}
// Test 2: Check [Post] type for equality: OK
let arrayOfPosts1: [Post] = [ post1, post2 ]
let arrayOfPosts2: [Post] = [ post1, post2 ]
if arrayOfPosts1 == arrayOfPosts2 {
print("equal arrays of post")
}
// Test 3: Check Result<Post> type for equality: OK
let result1: Result<Post> = Result<Post>.success(result: post1)
let result2: Result<Post> = Result<Post>.success(result: post2)
if result1 == result2 {
print("equal results of post")
}
// Test 4: Check Result<[Post]> type for equality: ERROR
// Compiler error: "Type '[Post]' does not conform to protocol 'Equatable'"
let arrayResult1: Result<[Post]> = Result<[Post]>.success(result: arrayOfPosts1)
let arrayResult2: Result<[Post]> = Result<[Post]>.success(result: arrayOfPosts2)
if arrayResult1 == arrayResult2 {
print("equal results of array of posts")
}
}
最佳答案
Swift 4.1 更新:
随着在 Swift 4.1 中引入条件一致性,Array
现在符合 Equatable
,因此应该可以解决该问题,而无需诉诸任何变通方法。
此外,Swift 现在允许类型自动合成 Equatable
一致性,前提是其所有成员都是 Equatable
,只需将 Equatable
一致性声明为原始类型定义的一部分(不是扩展)但没有实现其任何要求。这适用于提供关联值(如果有)是 Equatable
的枚举。
这个问题的代码现在可以更简洁地写成如下:
import Foundation
struct Post: Equatable {
let text: String
}
enum Result<T>: Equatable where T: Equatable {
case success(result: T)
case error
}
此代码将通过问题中指定的所有测试:
func test() {
// Test 1: Check Post type for equality: OK
let post1 = Post(text: "post")
let post2 = Post(text: "post")
if post1 == post2 {
print("equal posts")
}
// Test 2: Check [Post] type for equality: OK
let arrayOfPosts1 = [post1, post2]
let arrayOfPosts2 = [post1, post2]
if arrayOfPosts1 == arrayOfPosts2 {
print("equal arrays of post")
}
// Test 3: Check Result<Post> type for equality: OK
let result1 = Result<Post>.success(result: post1)
let result2 = Result<Post>.success(result: post2)
if result1 == result2 {
print("equal results of post")
}
// Test 4: Check Result<[Post]> type for equality: OK
let arrayResult1: Result<[Post]> = Result<[Post]>.success(result: arrayOfPosts1)
let arrayResult2: Result<[Post]> = Result<[Post]>.success(result: arrayOfPosts2)
if arrayResult1 == arrayResult2 {
print("equal results of array of posts")
}
}
这是输出:
test()
/*
prints:
equal posts
equal arrays of post
equal results of post
equal results of array of posts
*/
关于arrays - 为什么数组不符合 Equatable,而它的项在 Swift 中是 Equatable 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44988705/