2016-02-12 16 views
6

Bunu yapmaya çalışıyorum ama bazı sorunlarıSwift jenerik ertelendi sorunu

Bu

protocol CustomProtocol { 

} 

SubCustomProtocol

protocol SubCustomProtocol: CustomProtocol { 

} 

SubCustomProtocolImplementation

class SubCustomProtocolImplementation: SubCustomProtocol { 

} 
CustomProtocol

olduğunu alıyorum

Bu CustomClass

class CustomClass<P: CustomProtocol> { 

    var customProtocol: P? 

    func doSomething() { 

    } 

} 

SubCustomClass

class SubCustomClass<P: SubCustomProtocol>: CustomSubClass { 

} 

Ve BaseViewController

class BaseViewController<P: CustomProtocol, T: CustomClass<P>>: UIViewController { 

    var foo: T! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     foo?.doSomething() 
    } 
} 

Benim ViewController ben foo çağrı doğrultusunda

class ViewController<P: SubCustomProtocolImplementation, T: SubCustomClass<P>>: BaseViewController<P,T> { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
} 

nedir? .doSomething() it diyor 'T' 'CustomClass < 'P'>' bir alt tipi değil ve ne yaptığımı bilmiyorum yanlış

Ve ViewController bildiriminde o BaseViewController T CustomClass devralan gerektiriyor" diyor

< 'P'> "

Umarım bana yardımcı olabilirsin!

+0

Bu hatayı alamıyorum. En azından oyun alanında. Tam bağlamı sağladığınızdan emin misiniz? – iyuna

+0

@Iyuna Üzgün ​​bir şey kaçırıyordum! –

+1

sorusunu düzenledim Görünüşe göre, bir tip parametresi genel bir sınıf tarafından kısıtlanamaz. Bir sınıf veya bir protokol tarafından kısıtlanabilir. – CouchDeveloper

cevap

2

Foo var türünüzü CustomClass<P> olarak belirtmek isterseniz, bunun yerine aşağıdakileri yapmanız gerekir.

class ViewController<P: CustomProtocol>: UIViewController { 

    var foo: CustomClass<P>? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     foo?.doSomething() 
    } 
} 
+0

Teşekkürler, çok iyi açıklamıyordum, sorumu tekrar düzenledim –