#selector(delete(_:)) 在 uitextfield 中崩溃 swift

#selector(delete(_:)) crash in uitextfield swift

每当我在 uitextfield 中键入任何文本并长按该文本并在 delete 上长按 select 时,它就会崩溃。崩溃时它显示“由于未捕获的异常 'NSInvalidArgumentException',正在终止应用程序,原因:'-[App_Name.HomeVC 删除:]:无法识别 select 或发送到实例 0x14e18ba00'" 。我该如何解决这个崩溃问题。下面是 swift 代码。

    class HomeVC: UIViewController{
    @IBOutlet weak var txtSearch: UITextField!
override func viewDidLoad() {
        super.viewDidLoad()
self.txtSearch.addTarget(self, action: #selector(self.txtChanged(_:)), for: .editingChanged)
@objc func txtChanged(_ sender : UITextField){
        print(txtSearch.text!)}
}
    override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(delete(_:)) || action == #selector(cut(_:))
                {
                    return true
                } else if action == Selector(("_lookup:")) || action == Selector(("_share:")) || action == Selector(("_define:")) {
                    return false
                }
                return super.canPerformAction(action, withSender: sender)
            }
    }

添加删除功能:

class HomeVC: UIViewController{
    @IBOutlet weak var txtSearch: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.txtSearch.addTarget(self, action: #selector(self.txtChanged(_:)), for: .editingChanged)
    }

    @objc func txtChanged(_ sender : UITextField){
        print(txtSearch.text!)
    }


    override func delete(_ sender: Any?) {
        txtSearch.text = ""
    }

    override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(delete(_:)) || action == #selector(cut(_:))
            {
                return true
            } else if action == Selector(("_lookup:")) || action == Selector(("_share:")) || action == Selector(("_define:")) {
                return false
            }
            return super.canPerformAction(action, withSender: sender)
        }
    }