Skip to content
tezvyn:
Menu

Selectors: Objective-C's Function Pointers in Swift

Source: developer.apple.comEasyHow cards are made

Selectors: Objective-C's Function Pointers in Swift

A selector is a lightweight name for a method, not a direct pointer. Swift uses it to call Objective-C code dynamically at runtime. It's common in UIKit for target-action patterns, like button taps. The footgun: forgetting @objc causes a runtime crash.

Why it exists

Swift needs a way to interact with the vast number of existing Apple frameworks written in Objective-C. These frameworks rely on a dynamic runtime for features like the target-action pattern. Selectors are the bridge that allows static Swift code to call these dynamic Objective-C APIs.

The mental model

A selector is a name, not a direct function pointer. Imagine it's a person's name written on a note. To deliver a message, you don't go to a pre-defined address; you ask a central directory (the Objective-C runtime) to find the person with that name. This lookup happens when the code runs, making it dynamic but also riskier than a direct, compile-time checked call.

How it works

In Swift, you create a selector with the #selector() syntax, like #selector(myButtonTapped). The Swift compiler verifies that a method with that name exists in the current scope. However, for the Objective-C runtime to find it later, the method itself must be exposed using the @objc attribute. This attribute tells the compiler to generate the necessary metadata that the Objective-C runtime needs to discover the method by its string name.

When to use it

You must use selectors when an API from an Objective-C framework requires one. Three common places are: first, the target-action pattern in UIKit and AppKit, such as for UIButton taps or UIGestureRecognizer events; second, creating a Timer that fires repeatedly; and third, registering for notifications with NotificationCenter.

When not to use it

For any new, pure-Swift code, avoid selectors. Prefer modern Swift patterns like closures or Combine publishers. Closures are type-safe, checked at compile time, and capture their context, which avoids the runtime risks and indirection associated with selectors. Only use selectors when interoperability with an older Objective-C API forces you to.

One canonical example

The most common use is handling a button tap in UIKit. You create a button and tell it what to call when tapped using addTarget. The action parameter requires a selector, like myButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside). The corresponding method, handleTap, must be marked @objc func handleTap() { ... }. If you forget the @objc attribute, your code will compile, but the app will crash with an "unrecognized selector sent to instance" error when you tap the button.

Interview question

What is the primary purpose of marking a Swift method with @objc when using it as a selector for an Objective-C API?

  • a.To prevent the Swift compiler from optimizing away the method during compilation.
  • b.To make the method discoverable by the Objective-C runtime at execution.Correct
  • c.To convert the method into a block or closure that Objective-C can execute.
  • d.To enable compile-time type checking for the selector's signature.
Why?

The @objc attribute is crucial because it generates the necessary metadata for the Objective-C runtime to dynamically discover and invoke the Swift method by its string name (selector) at runtime. Without it, the Objective-C runtime cannot find the method, leading to a crash, unlike compile-time type checking which is a benefit of closures, not selectors.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles