簡體   English   中英

在擴展中的弱屬性上添加 didSet 觀察者

[英]Add didSet observer on a weak property in an extension

我正在嘗試制作一個 UITextField 擴展,它在設置委托時執行附加功能。

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

您不能使用擴展覆蓋委托屬性,您需要創建子類:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM