How to use swift 2 performSelector family of API?

With xcode 7 b4, swift 2.x now supports a selector.

How to use the API selector instead of the string typed "functionName:"?

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

@selector () in Swift? This question is similar, but it refers to a quick 1.x lack of selector support.

+4
source share
2 answers

Update:
You will be pleased to learn that with Swift 2.2 and Xcode 7.3 it can now warn when there is no method declaration for a particular selector. A new syntax has appeared. You should no longer define selectors as "myMethodToCall:"or Selector("myMethodToCall:"). Define them like this:

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

Xcode will suggest converting existing selectors to this new syntax. Hooray!

:
Xcode 7.2, , . Selector 'stringy'.

. :
API, , API performSelector. :

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

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

Screenshot for apple document

Apple Xcode 7.3

#selector expression, :

#selector(Class.Method)

:

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

Source: https://habr.com/ru/post/1599009/


All Articles