Swift version: 5.6
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 Timer
and 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 Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.