0

I don't seem to be able to remove local class functions from each of my classes because I can't figure out how to use #selector to call a function that's in another class (when Self is a required parameter).

Presently I call a local function which calls out just fine:

class Tickets: UIViewController {

    override func viewWillAppear(_ animated: Bool) {

        NotificationCenter.default.addObserver(self, selector: #selector(setDataIcon), name: NSNotification.Name(rawValue: "ActiveSyncRequestInitiated"), object: nil)
}

    @objc func setDataIcon() {
        global.setDataIcon(self) // called via #selector above, works
    }
}

I'd rather remove the local function setDataIcon and call via selector as

    #selector(global.setDataIcon(self)) // doesn't compile!

But I get error "Argument of '#selector' does not refer to an '@objc' method, property, or initializer"

TomB
  • 116
  • 2
  • 5
  • So, global is another class? Can we see what global is? Also - you say you want to 'remove the local function `setDataIcon` and call the selector' -- where would you like to call that at? Are you trying to do something like a `lambda/closure`? – impression7vx Oct 21 '19 at 00:00
  • I want my addObserver call to look something like: NotificationCenter.default.addObserver(self, selector: #selector(global.setDataIcon(self)), name: NSNotification.Name(rawValue: "ActiveSyncRequestInitiated"), object: nil) – TomB Oct 21 '19 at 00:43
  • As for the contents of my global class function, it refers back to the source: source.navigationItem.rightBarButtonItem = dataIconItem // where source is the UIViewController that's calling it -- the "self" in selector that I'm having trouble with. – TomB Oct 21 '19 at 00:48
  • This is, in essence, your question. https://stackoverflow.com/questions/44390378/how-can-i-deal-with-objc-inference-deprecation-with-selector-in-swift-4. Pretty much, your current answer is the most correct way to deal with this. – impression7vx Oct 21 '19 at 00:57

1 Answers1

0

The #selector word can only call methods marked with @objc. If you remove that method, or the @objc qualifier, #selector cannot call it. That is why you're getting the error.

If you want a notification observed in Class A to execute code in Class B, I see a few options;

1.) Create an instance of Class B inside Class A, and have the code in setDataIcon inside Class A method call the function from your instance of Class B.

2.) In setDataIcon, post a notification that is observed by Class B, and then register Class B to observe that notification and have Class B execute code upon observance.

3.) Use delegate methods. Make sure that before you register to observe the notification, you create an instance of Class B and assign it to the delegate of Class A. Then, inside setDataIcon, have the delegate (which is Class B) call the delegate method.

Looking at your comments, if 'global' is an instance of Class B inside Class A, the setDataIcon method inside of global needs to also be qualified with @objc. (option 1)

jkraft
  • 1
  • 1