Ale*_*lex 7 arrays generics enums swift swift3
更新:
从包含Swift 4.1的Xcode 9.3开始,数组相等性按预期工作,原始问题中的代码编译时没有错误.
但是,请参阅接受的答案,因为它提供了更好,更现代的解决方案.
原始问题如下:
当我尝试用类型声明一个通用枚举的实例时[Post],我得到一个错误说
类型'[Post]'不符合协议'Equatable'
这是无稽之谈,因为Post符合Equatable并且我实际上可以比较两个[Post]没有编译错误的实例?
在下面的例子,我延长Post和Result<T>类型的Equatable,然后我做了一些测试:
Post类型:好的[Post]类型:好的Result<Post>类型:好的Result<[Post]>类型:ERRORimport 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")
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.1更新:
随着Swift 4.1中的条件一致性的引入,Array现在符合Equatable,因此无需使用任何变通办法即可解决问题。
而且,只要通过将一致性声明为原始类型定义(而不是扩展名)的一部分,而无需实现其任何要求,Swift现在允许类型自动合成Equatable一致性,前提是其所有成员均为。如果提供的关联值是,则此方法适用于枚举。EquatableEquatableEquatable
现在,可以更简洁地编写此问题的代码,如下所示:
import Foundation
struct Post: Equatable {
let text: String
}
enum Result<T>: Equatable where T: Equatable {
case success(result: T)
case error
}
Run Code Online (Sandbox Code Playgroud)
此代码将通过问题中指定的所有测试:
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")
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
test()
/*
prints:
equal posts
equal arrays of post
equal results of post
equal results of array of posts
*/
Run Code Online (Sandbox Code Playgroud)