Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift4: respondstoSelector not working

Following code works perfectly in swift 3.x but not in swift 4.

let selector = "managerDidDetectedStation:"
let observer = <UIViewController subclass object>
let station = <Station Object>

if observer.responds(to: Selector(selector) {         
    observer.perform(Selector(selector), with: station)
}

observer.responds(to: Selector(selector) always returns false. Does anybody aware of any change in this api in swift4?

like image 888
Abid Hussain Avatar asked Dec 13 '22 20:12

Abid Hussain


1 Answers

You need to add @objcMembers at top of observer class

For Ex. if observer is a object of XYZClass class then write

@objcMembers
class XYZClass : NSObject
{
  // Your Stuff
}

OR

Another way is to put @objc to start of your method like below.

 @objc func managerDidDetectedStation...
like image 121
iPatel Avatar answered Dec 16 '22 10:12

iPatel