当将项目代码更新为 swift 4 时,add.target 方法出现一些错误
我该如何解决这个错误?
//swift3
var chatLogController: ChatLogController? {
didSet {
sendButton.addTarget(chatLogController, action: #selector(ChatLogController.handleSend), for: .touchUpInside)
uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: chatLogController, action: #selector(ChatLogController.handleUploadTap)))
}
}
提示消息告诉你该做什么,在声明你的函数之前添加@obj
。
@objc func handleSend(_ sender: UIGestureRecognizer){
...
}
原因是因为:
In Objective-C, a selector is a type that refers to the name of an
Objective-C method. In Swift, Objective-C selectors are represented by
the Selector
structure, and can be constructed using the #selector
expression. To create a selector for a method that can be called from
Objective-C, pass the name of the method, such as
#selector(MyViewController.tappedButton(_:))
. To construct a selector for a property’s Objective-C getter or setter method, pass the
property name prefixed by the getter:
or setter:
label, such as
#selector(getter: MyViewController.myButton)
.
阅读更多 here , 在 Apple 文档中。