2
下面的代码,斯威夫特:引用泛型类
protocol TestType : AnyObject {
}
class Generic<TestType> : NSObject {
private let filterFunction : (TestType,String) -> Bool
init(filter: @escaping (TestType,String) -> Bool) {
filterFunction = filter
}
}
class Parent : UIViewController {
public var generic : Generic<TestType>!
}
class Child : Parent {
override func viewDidLoad() {
// ERROR THIS LINE
generic = Generic<Object>(filter: { (str1, str2) -> Bool in
return true
})
}
}
class Object : TestType {
}
的错误是:
Cannot assign value of type 'Generic<Object>' to type 'Generic<TestType>!'
我尝试了很多东西,比如typealias,但不能编译代码。 问题是我不想要Parent<TestType>
或Child<TestType>
类,因为我想能够在IB中使用它。
我怎么能存储在母公司通用的基准,并在儿童初始化(动态,通过设置具体TestType像物体或其他)
比较[夫特通用胁迫误解](http://stackoverflow.com/q/41976844/2976878)&[如何存放Class类型的值< ClassImplementingProtocol>在Swift中类型为\ [String:Class \]的词典中?](http://stackoverflow.com/q/38590548/2976878) - “Generic
为什么不创建一个'Generic'实例? –
Hamish
Thx @Hamish,你的帖子非常有帮助。在我的代码中,父类是具有一些搜索功能的扩展TableviewController。我希望我的子控制器能够继承这些功能,并根据这些项目给父视图控制器提供项目和过滤器的列表,以便很好地工作。 –