3

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)))
    }
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Habib Alejalil
  • 435
  • 1
  • 4
  • 17
  • "get some error" Get _what_ error? – matt Sep 24 '17 at 17:30
  • this error "Argument of '#selector' refers to instance method 'handleSend()' that is not exposed to Objective-C" – Habib Alejalil Sep 24 '17 at 17:36
  • 1
    Well there's your answer. Just read what the error message says! Put `@objc` in front of your `func handleSend...` (which you didn't even _show_ in your question, for heaven's sake). – matt Sep 24 '17 at 17:41
  • but how? i don't use func on this line of code – Habib Alejalil Sep 24 '17 at 18:11
  • No, you use it when you declare it. But, as I said, you didn't show that. Find where you do say `func handleSend` and do what I said to do. – matt Sep 24 '17 at 18:26

1 Answers1

9

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.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Rashwan L
  • 38,237
  • 7
  • 103
  • 107