2015-11-19 76 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

最终,我试图看看如何依次调用多个代表(而不是被限制为一个)。然而无论如何,观察者模式在这种情况下更强大。 –