2017-06-13 41 views
-1

我有一個泛型類:通知中心觀測與泛型類

open class GenericClass<T:MyClass>: NSObject { 

    public init(_ myParam:Int) {    
     NotificationCenter.default.addObserver(self, selector: #selector(self.someFunc), name: .MyName, object: nil) 
    } 

    func someFunc() { 

    } 
} 

但我不知道,那些代碼不起作用。我得到錯誤:

'self' used before super.init call

回答

0

你只需要調用NSObject初始化劑(你繼承的類):

open class GenericClass<T:MyClass>: NSObject { 

    public init(_ myParam:Int) { 
     super.init() 
     NotificationCenter.default.addObserver(self, selector: #selector(self.someFunc), name: .MyName, object: nil) 
    } 

    func someFunc() { 

    } 
}