如何使协议关联类型要求协议继承而不是采用协议

8
在我的Swift项目中,我有一个使用协议继承的情况,如下所示:
protocol A : class{

}

protocol B : A{

}

我接下来想要实现的是声明另一个带有关联类型的协议,这个关联类型必须继承自协议A。如果我试图这样声明:

protocol AnotherProtocol{
    associatedtype Type : A
    weak var type : Type?{get set}
}

代码编译没有错误,但在尝试使用AnotherProtocol时,出现了以下情况:

class SomeClass : AnotherProtocol{

    typealias Type = B
    weak var type : Type?
}

编译失败,错误表示SomeClass不符合AnotherProtocol。如果我理解正确,这意味着在我尝试声明并询问如何声明从协议A继承的关联类型时,B没有采用A
我基于以下事实做出上述假设,因为以下情况可以成功编译。
class SomeDummyClass : B{

}

class SomeClass : AnotherProtocol{

    typealias Type = SomeDummyClass
    weak var type : Type?
}
1个回答

6
这很有趣。似乎一旦你限制了给定协议中关联类型的类型,你需要在该协议的实现中提供一个具体类型(而不是另一个协议类型),这就是为什么你的第二个示例起作用的原因。
如果您删除关联类型上的 A 约束,则第一个示例将起作用(减去无法在非类类型上使用 weak 的错误,但这似乎与此无关)。
话虽如此,我似乎找不到任何文件来证实这一点。如果有人能找到支持这一点(或完全争辩反对它)的资料,我会很高兴知道!
为了使您的当前代码工作,您可以使用泛型。这实际上可以一石二鸟,因为您的代码现在将编译,并且您将从泛型带来的增加的类型安全性中受益(通过推断传递给它们的类型)。
例如:
protocol A : class {}
protocol B : A {}

protocol AnotherProtocol{
    associatedtype Type : A
    weak var type : Type? {get set}
}

class SomeClass<T:B> : AnotherProtocol {
    typealias Type = T
    weak var type : Type?
}
编辑:在您的特定情况下,上述解决方案可能不适用,因为您希望避免使用具体类型。如果对其他人有用,我将保留此处。


在您的特定情况下,您可能可以使用类型擦除创建一个伪具体类型,以用于您的B协议。 Rob Napier有一篇很好的文章关于类型擦除。

在这种情况下,这是一个有点奇怪的解决方案(因为类型擦除通常用于包装具有associatedtypes的协议),而且它肯定不如上面的解决方案受欢迎,因为您必须重新实现每个方法的'proxy'方法在您的AB协议中-但它应该适合您。

例如:

protocol A:class {
    func doSomethingInA() -> String
}

protocol B : A {
    func doSomethingInB(foo:Int)
    func doSomethingElseInB(foo:Int)->Int
}

// a pseudo concrete type to wrap a class that conforms to B,
// by storing the methods that it implements.
class AnyB:B {

    // proxy method storage
    private let _doSomethingInA:(Void)->String
    private let _doSomethingInB:(Int)->Void
    private let _doSomethingElseInB:(Int)->Int

    // initialise proxy methods
    init<Base:B>(_ base:Base) {
        _doSomethingInA = base.doSomethingInA
        _doSomethingInB = base.doSomethingInB
        _doSomethingElseInB = base.doSomethingElseInB
    }

    // implement the proxy methods
    func doSomethingInA() -> String {return _doSomethingInA()}
    func doSomethingInB(foo: Int) {_doSomethingInB(foo)}
    func doSomethingElseInB(foo: Int) -> Int {return _doSomethingElseInB(foo)}
}

protocol AnotherProtocol{
    associatedtype Type:A
    weak var type : Type? {get set}
}

class SomeClass : AnotherProtocol {
    typealias Type = AnyB
    weak var type : Type?
}

class AType:B {
    // implement the methods here..
}
class AnotherType:B {
    // implement the methods here..
}

// your SomeClass instance
let c = SomeClass()

// set it to an AType instance
c.type = AnyB(AType())

// set it to an AnotherType instance
c.type = AnyB(AnotherType())

// call your methods like normal
c.type?.doSomethingInA()
c.type?.doSomethingInB(5)
c.type?.doSomethingElseInB(4)

现在,您可以使用AnyB类型来替代使用B协议类型,而不会使其变得更加类型限制。

谢谢您的回答,不幸的是,在我的情况下使用“weak”非常重要,这也是引发这个问题的原因。感谢您提供泛型的解决方案,但仍然不是我想要的。 - Zell B.
@ZellB。您仍然可以在泛型中使用weak,只需在创建SomeClass实例时提供type属性的实际具体类型即可,例如:let c = SomeClass<符合B协议的您的类型>(),或者这不是您能够做到的吗? - Hamish
我实际上想避免使用具体类型,而是使用协议作为类型。采用 AnotherProtocol 的类不应该能够访问在协议中未指定的类型,并且声明 let c = SomeClass<B>() 也不起作用。 - Zell B.
@ZellB。啊,好的,如果是这样的话,您可以使用type erasure来处理您的协议B,以使其成为伪具体类型(尽管我自己对此是否能解决您的问题有所怀疑)。 - Hamish
@ZellB。我已经添加了一个示例,说明您如何使用类型抹消来解决您的问题。 - Hamish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接