Skip to content
tezvyn:
Menu

Refining Objective-C APIs for Swift with NS_SWIFT_NAME

Source: developer.apple.comHardHow cards are made

Refining Objective-C APIs for Swift with NS_SWIFT_NAME

NS_SWIFT_NAME gives Objective-C code a Swifty alias without changing the original API. Use it to modernize legacy frameworks by removing prefixes for Swift consumption.

Why it exists

Objective-C and Swift have different naming conventions. Objective-C favors long, descriptive names with prefixes (like NS or a custom prefix) to avoid collisions. Swift uses modules for namespacing, making these prefixes redundant. NS_SWIFT_NAME was created to bridge this gap, allowing old APIs to feel new in Swift without breaking existing Objective-C code.

The mental model

Think of NS_SWIFT_NAME as a "stage name" for your Objective-C code when it performs in a Swift environment. The code has its legal, birth name (the Objective-C name) that it uses backstage and in all official Objective-C documents. But when it's on the Swift stage, it uses its slicker, more modern stage name.

How it works

You add the NS_SWIFT_NAME macro to your Objective-C header (.h) file, right after the declaration of a class, enum, protocol, or function. You provide the desired Swift name as an argument to the macro. For example, typedef NS_ENUM(NSInteger, TSKTaskState) { ... } NS_SWIFT_NAME(Task.State);. When the Swift compiler imports this header, it replaces all occurrences of TSKTaskState with Task.State.

When to use it

This is a key tool for incremental migration from Objective-C to Swift. Use it to remove class prefixes (e.g., ACMEWidget becomes Widget), create more ergonomic method signatures, and nest related types like enums inside a class for better namespacing in Swift (as seen in the Task.State example).

When not to use it

Don't use this for new, Swift-first codebases. If you don't need to maintain Objective-C compatibility, just write idiomatic Swift from the start. Also, avoid creating drastically different names that hide the connection to the original API; the goal is refinement, not complete obfuscation, which can make debugging harder.

One canonical example

To rename an Objective-C class, you'd write this in the header: @interface MYAppController : NSObject @end NS_SWIFT_NAME(AppController). In your Swift code, you can now instantiate it using let controller = AppController(). However, in any .m file, you must still use MYAppController *controller = [[MYAppController alloc] init];.

Interview question

What is the primary function of NS_SWIFT_NAME when applied to an Objective-C API?

  • a.To enable Objective-C code to call Swift methods using their native Swift names.
  • b.To ensure that Objective-C APIs are only exposed to Swift if they already follow Swift's module-based naming.
  • c.To provide a distinct, idiomatic Swift name for an Objective-C API without changing its underlying Objective-C definition.Correct
  • d.To automatically refactor the Objective-C API's source code to match Swift naming conventions.
Why?

The card explicitly states NS_SWIFT_NAME gives Objective-C code a "Swifty alias without changing the original API" and acts as a "stage name" for Swift consumption. Option D is incorrect because NS_SWIFT_NAME creates an alias for Swift, but it does not modify or refactor the actual Objective-C source code; the Objective-C code retains its original naming.

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