假设我有这样的协议和结构:

protocol A {
    var someType: UnrelatedProtocol.Type { get }
}

struct B<T: UnrelatedProtocol> {
    var anotherThing: T?
}

我想这样一起使用:
struct C<T: A> {
    typealias SomeThing = (B<T.someType>) -> Void
}

这在斯威夫特有可能吗?我一直在玩这个,但没能完全正确。也许不可能,但我觉得我应该能做这样的事情。
谢谢!

最佳答案

首先我们有

protocol UnrelatedProtocol { }

现在我们需要在协议中定义一个关联类型A
protocol A {
    associatedtype SomeType: UnrelatedProtocol
    var someType: SomeType { get }
}

最后
struct B<T: UnrelatedProtocol> {
    var anotherThing: T?
}

struct C<T: A> {
    typealias Logic = (B<T.SomeType>) -> ()
}

09-11 18:35
查看更多