How to Update addTarget method to swift 4

Viewed 12675

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)))
    }
}
1 Answers

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 the getter: or setter: label, such as #selector(getter: MyViewController.myButton).

Read more here, at Apples documentation.

Related