1
這是我的遊樂場:斯威夫特嵌套泛型
class A {
required init() { // in order to use X() this init must be required
}
}
class B<X: A> {
required init() {
X()
}
}
class C<X: A, Y: B<X>> {
init() {
Y() // Error here: 'X' is not a subtype of 'A'
}
}
C()
這有可能在斯威夫特有哪些?我究竟做錯了什麼?
更新
我真正想要的是這個(遊樂場與此代碼崩潰):
import UIKit
import CoreData
class MyEntity: NSManagedObject {}
class GenericCell<X: NSManagedObject>: UITableViewCell {
func doSomething(entity: X) {}
}
class MyEntityCell: GenericCell<MyEntity> {}
class C<X: NSManagedObject, Y: GenericCell<X>>: UITableViewController {
init() {
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// here I should have Y = MyEntityCell
tableView.registerClass(Y.self, forCellReuseIdentifier: "Cell")
}
}
C<MyEntity, MyEntityCell>()
我沒有問題,在編譯。也許你的代碼的其他部分會造成問題? – Sulthan
@Sulthan你可以嘗試使用代碼中的任何類嗎? – gfpacheco
完成。看到我的答案。 – Sulthan