1

I have a method written in objective-c. It's like this.

-(void) onLongPress:(UILongPressGestureRecognizer *) longPressGestureRecognizer {

I want to call this method from my swift file.So I did like this.

var longPressGuestureRecognizer = UILongPressGestureRecognizer(target: metrixUIViewController, action: #selector(metrixUIViewController.onLongPress(longPressGestureRecognizer:)))

But it says MetrixUIViewController has no member onLongPress. How can I solve this? Please help me.

Randi
  • 639
  • 2
  • 6
  • 23
  • You need to declare your method in the `MetrixUIViewController.h` file. Like that: `-(void) onLongPress:(UILongPressGestureRecognizer *) longPressGestureRecognizer;` – Jimmy James Dec 04 '17 at 08:44
  • @JimmyJames But I can call it from an objective-c class – Randi Dec 04 '17 at 08:45
  • You need to declare it so you can access your method outside of your class. If it's not declared, your method in 'private' so you can only access it from your class with `self` – Jimmy James Dec 04 '17 at 08:47
  • But I am accessing it from another objective c viewcontroller. why its not accessible from swift class? – Randi Dec 04 '17 at 08:48
  • Look at this stack post: https://stackoverflow.com/a/24005242/2894160 it may help you – Jimmy James Dec 04 '17 at 08:51
  • Maybe it's a typo in the question but the target should use a static class : MetrixUIViewController ( with a capital letter ). Moreover, be sure to include your .h file in the bridging-header. – CZ54 Dec 04 '17 at 08:51
  • what is your class name (taking letter case into account)? are you sure that you are not referring to a _class_ method but an _instance_ method when your create the `UILongPressGestureRecognizer`? – holex Dec 04 '17 at 08:59

2 Answers2

2

First check your bridging header is it correct?
Your onLongPress method will be public. should be declared in .h and .m file

Change at below

var longPressGuestureRecognizer = UILongPressGestureRecognizer(target: metrixUIViewController, action: #selector(metrixUIViewController.onLongPress(_:)))
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • This worked when I declare the method in the .h file. But I don't understand why it worked when I call it within an objective-c Viewcontroller. Coult you please explain? – Randi Dec 04 '17 at 08:57
  • You can not direct access method from another class even you are using objective c. for access you must need define method in .h and declare body of method in .m file – iPatel Dec 04 '17 at 09:01
2

onLongPress must be written in .m File not declared in .h please declare.

Divyam shukla
  • 2,038
  • 14
  • 18