作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要在我的泛型基类中传递一个类型。
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/
我是一名优秀的程序员,十分优秀!