Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override of instance method form extension depends on deprecated inference of '@objc'

Tags:

ios

swift

swift4

I am trying to convert my code (written in Swift 3) to Swift 4, for that I am adding @objc where needed. Xcode has done quite a good job to automatically fix them but I am struggling with a few (all using the same 2 methods), where Xcode can't help, it just puts @objc somewhere in my code.

I am overriding a method called navbarRightButtonAction(button:) like this in my ViewController class.

class ViewController: PBViewController {
    override func navbarRightButtonAction(button: PBAdaptiveButton) {
        ...
    }
}

This is where I get the warning saying:

Override of instance method 'navbarRightButtonAction(button:)' from extension of PBViewController depends on deprecated inference of '@objc'

Then I thought the problem us be in the PBViewController class which looks like this:

extension PBViewController: PBNavigationBarDelegate {
    func navbarRightButtonAction(button: PBAdaptiveButton) {
        print("Override this method")
    }
}

So I added @objc func navbarRightButtonAction(button: PBAdaptiveButton) but it didn't help.
Then I looked into the PBNavigationBarDelegate protocol

protocol PBNavigationBarDelegate {
    func navbarRightButtonAction(button:PBAdaptiveButton)
}

I added @objc protocol PBNavigationBarDelegate but it didn't help either.
I have no other idea what to do to fix the deprecation warning.

like image 267
Codey Avatar asked Oct 18 '17 21:10

Codey


1 Answers

Put @objc or @nonobjc in front of the extension:

@objc extension PBViewController: PBNavigationBarDelegate

Take a look at Limiting @objc Inference, SE-0160 at Swift Evolution for more details. It contains the following example regarding extensions:

Enabling/disabling @objc inference within an extension

There might be certain regions of code for which all of (or none of) the entry points should be exposed to Objective-C. Allow either @objc or @nonobjc to be specified on an extension. The @objc or @nonobjc will apply to any member of that extension that does not have its own @objc or @nonobjc annotation. For example:

class SwiftClass { }

@objc extension SwiftClass {
  func foo() { }            // implicitly @objc
  func bar() -> (Int, Int)  // error: tuple type (Int, Int) not
      // expressible in @objc. add @nonobjc or move this method to fix the issue
}

@objcMembers
class MyClass : NSObject {
  func wibble() { }    // implicitly @objc
}

@nonobjc extension MyClass {
  func wobble() { }    // not @objc, despite @objcMembers
}
like image 97
leanne Avatar answered Sep 18 '22 22:09

leanne