Selectors are effectively the names of methods on an object or struct, and they are used to execute some code at runtime. They were common in Objective-C, but the earliest versions of Swift didn't include some core selector functionality so their use declined for a while. That functionality (things like
performSelector(inBackground:)) has since been restored.
In Swift, selectors are most commonly found when working with the target/action paradigm that you find in classes such as
UIBarButtonItem. For example, when you create a timer you need to tell it who to notify when the timer fires (the target) and what selector should be called (the action). The same goes for bar button items: when the button is tapped, what selector should be called, and on what object?
Sponsored You’re already busy updating your app for Swift 4.2 and iOS 12, so why not let Instabug help you find and fix bugs? Add just two lines of code to your project and receive comprehensive reports with all the feedback you need to ship a world-class app – click here to learn more!
Available from iOS 7.0
Did this solution work for you? Please pass it on!
Other people are reading…
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Learn Swift faster!
Take your Swift learning to the next level: buy the Hacking with Swift e-book and get bonus material to help you learn faster!