3

我需要用于NOT类中的任何 Equatable项目的容器(例如,从情节提要开始的 UI 类)。我需要这样Generic

var items: [Equatable]?

但它不起作用,Equatable需要Generic。不存在公共Equatable类的问题。


好的 - 转到通用!但如果我这样做

class Item<Value: Equatable>: Equatable {
   var value: Value
   init(_ value: Value) {
       self.value = value
   }
   //Equatable
   public static func ==(lhs: Item, rhs: Item) -> Bool {
       return (lhs.value == rhs.value)
   }
}

那么我将被迫在我的 nonGeneric-UI 类中指定类型。像这样

var items: [Item<WhatShouldBeHere?>]?

但是我们又遇到了不存在公共Equatable类的问题


All Equatable 容器的任何解决方案?

4

1 回答 1

6

代替存在类型,您需要使用类型橡皮擦:

public struct AnyEquatable: Equatable {
    public let value: Any
    private let equals: (Any) -> Bool

    public init<E: Equatable>(_ value: E) {
        self.value = value
        self.equals = { ($0 as? E) == value }
    }

    public static func == (lhs: AnyEquatable, rhs: AnyEquatable) -> Bool {
        return lhs.equals(rhs.value) || rhs.equals(lhs.value)
    }
}

示例用法:

let items = [
    AnyEquatable(1),
    AnyEquatable(1.23),
    AnyEquatable(true),
    AnyEquatable("some string")
]

let desired = "some string"
let desiredAsAnyEquatable = AnyEquatable(desired)
let desiredDescription = String(reflecting: desired)

for item in items {
    let itemDescription = String(reflecting: item.value)
    let isEqualToDesired = item == desiredAsAnyEquatable
    print("\(itemDescription) is \(isEqualToDesired ? "": "not ")equal to \(desiredDescription)")
}

示例输出:

1 不等于“某个字符串”

1.23 不等于“某个字符串”

true 不等于“某个字符串”

“某个字符串”等于“某个字符串”

于 2017-09-18T21:41:13.550 回答