1

As of Swift 2.2, the following code gives the warning:

No method declared with Objective-C selector'sync'

if let tabBarController = segue.destinationViewController as? TabBarController {
  tabBarController.navigationItem.rightBarButtonItem = 
    UIBarButtonItem(title: "Upload", 
                    style: .Plain, 
                    target: tabBarController, 
                    action: "sync")

What should I replace action: "sync"it with to get rid of the warning?

I've tried:

Selector("sync") // The Xcode provided fix which yields the same warning
#selector(tabBarController.sync()) // Error: Argument of '#selector' does not refer to initializer or method 
Selector(tabBarController.sync()) // No error/warning but doesn't fire sync function
doovers
  • 8,545
  • 10
  • 42
  • 70

2 Answers2

12

For solving of your issue at first read new documentation about Selectors in Swift2.2.

Example: Use #selector(CLASS.sync) instead of Selector("sync"). Where CLASS it is actual class that contains this method.

And this was done due to this reason:

The use of string literals for selector names is extremely error-prone: there is no checking that the string is even a well-formed selector, much less that it refers to any known method, or a method of the intended class. Moreover, with the effort to perform automatic renaming of Objective-C APIs, the link between Swift name and Objective-C selector is non-obvious. By providing explicit "create a selector" syntax based on the Swift name of a method, we eliminate the need for developers to reason about the actual Objective-C selectors being used.

Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
0

I think you have misplaced the action function "sync". Keep it in your TabBarController because you have used TabBarController's instance as target. Code like below will work:

tabBarController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Upload", style: .Plain, target: tabBarController, action: "sync:")

keep below function in TabBarController:

func sync(sender: AnyObject){
    //your code here
}

Hope it solves your issue. :)

Bibek
  • 3,689
  • 3
  • 19
  • 28