2015-11-19 19 views
2

我正在試圖製作一個UITextField擴展,它在設置委託時執行其他功能。在擴展中的弱屬性上添加didSet觀察器

extension UITextField { 
    override weak public var delegate: UITextFieldDelegate? { 
     didSet { 
      print("Do stuff") 

     } 
    } 
} 

這失敗的三個錯誤:

'delegate' used within its own type 

'weak' cannot be applied to non-class type '<<error type>>' 

Property does not override any property from its superclass 

什麼我需要爲Do stuff改變在委託的設置要打印的?

回答

2

使用分機不能覆蓋委託財產,你需要創建子類:

class TextField: UITextField { 
    override weak var delegate: UITextFieldDelegate? { 
     didSet { 
      super.delegate = delegate 
      print("Do stuff") 
     } 
    } 
} 

但這似乎有點不對。你想達到什麼目的?

+0

最終,我試圖看看如何依次調用多個代表(而不是被限制爲一個)。然而無論如何,觀察者模式在這種情況下更強大。 –