1

这是交易,

我正在编写一个 SDK,我想将观察者声明为协议,而不是类或结构(它有点像“观察者/代表”混合体)。

我希望能够比较作为协议引用传入的两个参数,而不是它们实际上是 IRL 的具体类/结构。

我知道进行比较的“简单”方法是将协议限制为Hashableor Equatable,但我想避免给用户带来负担(这是一个 SDK)。

这是一个小操场,我的意思是:

protocol A {
    func AFunc() -> String
}

class APrime: A {
    func AFunc() -> String { "I AM GROOT" }
}

let variableA = APrime()
let variableB = APrime()

func compareTypes(_ inA: A, _ inB: A) -> String {
//    if inA == inB {
//        return ""
//    }
    return "not "
}

print("A is \(compareTypes(variableA, variableB))B.")

print("A is \(compareTypes(variableA, variableA))A.")

不修边幅的部分是compareTypes(_: A, _: A). 我需要弄清楚如何在不进入“Hacksylvania”的情况下比较它们,我可以通过在每个实例中比较 AFunc() 的地址来做到这一点。

预期的输出是:

A is not B.
A is A.

对于更“迅速”的方法有什么想法吗?我一定是只见树木不见森林。

4

1 回答 1

1

只是为此添加一些闭包,这是我解决此问题的方法:

protocol A {
    var uuid: Int { get } // This is the secret sauce. It will contain a unique UUID, associated with the instance.
    func AFunc() -> String
}

class APrime: A {
    let uuid: Int = Int.random(in: 0..<1000) // The UUID is initialized with the instance.
    func AFunc() -> String { "I AM GROOT" }
}

let variableA = APrime()
let variableB = APrime()
let variableC = variableA

func compareTypes(_ inA: A, _ inB: A) -> String {
    if inA.uuid == inB.uuid { // We compare UUIDs.
        return ""
    }
    return "not "
}

print("C is \(compareTypes(variableC, variableB))B.")

print("C is \(compareTypes(variableC, variableA))A.")

“uuid”变量通常是一个实际的UUID类型,但我不想在示例中导入 Foundation,所以我只是做了一个简单的 rand。它明白了这一点。

这输出:

C is not B.
C is A.

还有另一种方法(有时我也使用):

protocol B {
    func BFunc() -> String
    func amIThisOne(_ instanceToCompare: B) -> Bool // This is an identity comparator
}

class BPrime: B {
    func BFunc() -> String { "I AM GROOT'S BROTHER" }
    // We compare ourselves against the other instance, assuming it can be cast to our own type.
    func amIThisOne(_ inInstanceToCompare: B) -> Bool {
        guard let instanceToCompare = inInstanceToCompare as? Self else { return false }
        return self === instanceToCompare
    }
}

let variableD = BPrime()
let variableE = BPrime()
let variableF = variableD

print("D is \(variableE.amIThisOne(variableD) ? "" : "not ")E.")

print("D is \(variableD.amIThisOne(variableF) ? "" : "not ")F.")

哪个输出:

D is not E.
D is F.

这允许以更加程序化的方式比较实例。

怎么做

然后,当然,如果我们可以控制实例,我们就可以真正做到 Equatable 的事情(这需要操场导入 Foundation):

protocol C: Equatable {
    func CFunc() -> String
}

class CPrime: C {
    // This is actually not what I want, as I want to compare protocols, not conforming classes.
    static func == (lhs: CPrime, rhs: CPrime) -> Bool {
        guard let lhs = lhs as? Self else { return false }
        guard let rhs = rhs as? Self else { return false }

        return lhs === rhs
    }

    func CFunc() -> String { "I AM GROOT'S UDDER BROTHER" }
}

let variableG = CPrime()
let variableH = CPrime()
let variableI = variableG

print("G is \(variableG == variableH ? "" : "not ")H.")

print("G is \(variableI == variableG ? "" : "not ")I.")

哪个输出:

G is not H.
G is I.
于 2020-03-01T17:15:39.300 回答