How to Update addTarget method to swift 4
The hint message is telling you what to do, add @obj
before declaring your function.
@objc func handleSend(_ sender: UIGestureRecognizer){
...
}
The reason is because:
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 thegetter:
orsetter:
label, such as#selector(getter: MyViewController.myButton)
.
Read more here, at Apples documentation.
Habib Alejalil
Updated on June 24, 2022Comments
-
Habib Alejalil almost 2 years
when update project code to swift 4 get some error for add.target method how i can fix this error?
//swift3 var chatLogController: ChatLogController? { didSet { sendButton.addTarget(chatLogController, action: #selector(ChatLogController.handleSend), for: .touchUpInside) uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: chatLogController, action: #selector(ChatLogController.handleUploadTap))) } }