Я пытаюсь сделать это, но я получаю некоторые неприятностиSwift дженерики отложили выпуск
Это CustomProtocol
protocol CustomProtocol {
}
SubCustomProtocol
protocol SubCustomProtocol: CustomProtocol {
}
SubCustomProtocolImplementation
class SubCustomProtocolImplementation: SubCustomProtocol {
}
Это CustomClass
class CustomClass<P: CustomProtocol> {
var customProtocol: P?
func doSomething() {
}
}
SubCustomClass
class SubCustomClass<P: SubCustomProtocol>: CustomSubClass {
}
И мой BaseViewController
class BaseViewController<P: CustomProtocol, T: CustomClass<P>>: UIViewController {
var foo: T!
override func viewDidLoad() {
super.viewDidLoad()
foo?.doSomething()
}
}
Мои ViewController
class ViewController<P: SubCustomProtocolImplementation, T: SubCustomClass<P>>: BaseViewController<P,T> {
override func viewDidLoad() {
super.viewDidLoad()
}
}
В строке, где я называю Foo? .doSomething() его говорит что «T» не является подтипом «CustomClass <„Р“>», и я не знаю, что я делаю неправильно
И в декларации ViewController он говорит, что «BaseViewController требует , что T унаследован от CustomClass < 'P'> "
Надеюсь, вы можете мне помочь!
Я не получаю эту ошибку. По крайней мере, на детской площадке. Вы уверены, что предоставляете полный контекст? – iyuna
@Iyuna Мне не хватало чего-то жаль! Я отредактировал вопрос –
Кажется, параметр типа не может быть ограничен общим классом. Это может быть ограничено классом или протоколом. – CouchDeveloper