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

标签 ios swift generics

我想在 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/

相关文章:

arrays - 掉落(其中 : {}) remove(where: {}) function in Swift?

swift - JWT 验证算法 RSA 256 总是出错

c# - 类型参数约束是一个类

Java:通用类型转换中(缺乏)错误

ios - 可以使用 HKWorkoutBuilder 对象在 iPhone(没有配对的 watch )中收集锻炼数据吗?

ios - 如何限制对依赖Internet连接的ViewController的访问?

swift - 关闭音效

ios - 如何使用 Storyboard为一个 viewController 添加和编辑功能

ios - 架构 i386 的 undefined symbol : trying to add in AVFoundation

java - 奇怪的泛型问题