swift - 如何使协议(protocol)关联类型需要协议(protocol)继承而不是协议(protocol)采用

标签 swift swift-protocols associated-types protocol-inheritance

在我的 swift 项目中,我有一个使用协议(protocol)继承的案例,如下所示

protocol A : class{

}

protocol B : A{

}

接下来我要实现的目标是声明另一个具有关联类型的协议(protocol),该类型必须从协议(protocol) 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。如果我理解正确,这意味着 B 不采用 A 而我试图声明并询问您如何声明继承自协议(protocol) A 的关联类型?

我做出上述假设是基于下面的场景编译得很好

class SomeDummyClass : B{

}

class SomeClass : AnotherProtocol{

    typealias Type = SomeDummyClass
    weak var type : Type?
}

最佳答案

这很有趣。 似乎,一旦您在给定协议(protocol)中限制了 associatedtype 的类型,您需要在该协议(protocol)的实现中提供具体类型(而不是另一种协议(protocol)类型) – 这就是您的第二个示例起作用的原因。

如果删除关联类型上的 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 协议(protocol)创建伪具体类型。 Rob Napier has a great article关于类型删除。

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

例如:

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 协议(protocol)类型,而无需对其进行更多类型限制。

关于swift - 如何使协议(protocol)关联类型需要协议(protocol)继承而不是协议(protocol)采用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867827/

相关文章:

与多个 Web 服务快速通信

ios - 在今天的扩展中,我如何从主应用程序获取数据?

ios - Xcode Playground 运行时间太长

Swift 枚举 + 关联值 + 内联大小写检查

generics - 类型参数与 std::ops::BitXor 输出关联类型之间的类型不匹配

ios - 如何根据其内在内容调整 View 大小?

ios - 如何在一个 ViewController 中创建实现多协议(protocol)?

swift - 可以使用 Swift 中的实现者定义的功能扩展协议(protocol)吗?

swift - 使用 EXC_BAD_INSTRUCTION 在空扩展中断中声明符合 @objc 协议(protocol)

ios - Swift ViewModel 与 UITableViewCell 的关联类型