Chéyo
Chéyo

Reputation: 9367

How to use swift 2 performSelector family of APIs?

With the xcode 7 b4 swift 2.x now supports selectors.

How do I use the selector API instead of stringy typed "functionName:" ?

let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "functionName:")

@selector() in Swift? This question is similar but it references swift 1.x lack of selector support.

Upvotes: 4

Views: 1651

Answers (2)

Jayprakash Dubey
Jayprakash Dubey

Reputation: 36447

Swift 2.2 and Xcode 7.3 onwards the use of sting literals as selector in selector methods has been deprecated.

Screenshot for apple document

Refer Apple document for Xcode 7.3 release notes

You have to use #selector expression as shown below:

#selector(Class.Method)

Example :

myButton.addTarget(objButton, action: #selector(LoginViewController.checkLogin), userInfo: nil. repeats: false)

Upvotes: 0

Jordan H
Jordan H

Reputation: 55705

Update:
You will be happy to know that as of Swift 2.2 and Xcode 7.3, it can now warn when there is no method declaration for the defined selector. There is new syntax introduced. No longer shall we define selectors as "myMethodToCall:" or Selector("myMethodToCall:"). Define them like so:

#selector(ViewController.myMethodToCall(_:))
//or without a parameter
#selector(ViewController.myMethodToCall)

Xcode will offer to convert your existing selectors to this new syntax. Cheers!

Original:
There is no way to create a selector in Xcode 7.2 that would cause a warning to be shown in the case the provided function name does not exist. We still need to use the Selector struct with the 'stringy' function name.

On performSelector:
The API you're speaking of is the performSelector APIs. You can use it like so:

self.performSelector("functionName")
//or
self.performSelector(Selector("functionName"))

Upvotes: 4

Related Questions