gpt4 book ai didi

ios - 实现另一个参数的 Swift 泛型类参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:12 25 4
gpt4 key购买 nike

我想在 Swift 中实现一个带有两个参数的泛型类。一个必需的约束是第一个参数是一个协议(protocol),第二个参数必须实现这个协议(protocol)。

代码:

protocol Protocol {}

class Generic<T: Protocol, U> where U: T {

}

编译器错误说:

error: type 'U' constrained to non-protocol, non-class type 'T'

但这不是真的,因为 T 是一个协议(protocol)。

您知道如何实现吗?

最佳答案

认为你想要的是

protocol Protocol {} 
class Generic<T: Protocol> {
init(type: T.Type, instance: T) {

}
}

但是用例可以帮助我们理解您要实现的目标...

关于ios - 实现另一个参数的 Swift 泛型类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51375864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com