gpt4 book ai didi

ios - 如何在 Swift 泛型类中使用 AnyClass

转载 作者:搜寻专家 更新时间:2023-10-30 21:53:21 24 4
gpt4 key购买 nike

我需要在我的泛型基类中传递一个类型。

class SomeBaseClass<T: AnyClass> {
// Implementation Goes here
}

我收到以下错误:

Inheritance from non-protocol, non-class type 'AnyClass' (aka 'AnyObject.Type')

理想情况下,我想将“T”用作特定类型而不是 AnyClass,但 AnyClass 也可以。

谢谢

最佳答案

如果您希望类型是一个类,您应该使用 AnyObject

class SomeBaseClass<T: AnyObject> {
// Implementation Goes here
}

// Legal because UIViewController is a class
let o1 = SomeBaseClass<UIViewController>()

// Illegal (won't compile) because String is a struct
let o2 = SomeBaseClass<String>()

关于ios - 如何在 Swift 泛型类中使用 AnyClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32416023/

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