NS_SWIFT_NAME is recursive

Ilya Kulakov
1 min readMay 23, 2019

While working on the next version of ShortcutRecorder I pay special attention to how its API appears in Swift. Being written in Objective-C its extremely verbose and while default interoperability works well, small touches are needed here and there. NS_SWIFT_NAME to the rescue!

In particular I needed the following Objective-C declarations

to appear in Swift like

After reading the docs my first attempt was

And it did not work (: Before falling into despair I decided to read the docs again, this time carefully

Why was the Swift name for SandwichBreadType SandwichPreferences.BreadType and not Sandwich.Preferences.BreadType I wondered. A typo? Easy enough to find out

NS_SWIFT_NAME is recursive!

And it’s not explicitly mentioned anywhere. Apparently, the proper way to use the macro isNS_SWIFT_NAME(<ObjC Name>.<Swift Name>) The compiler then recursively resolves <ObjC Name> for as long as there are matching annotated declarations.

It’s not helping, that NS_SWIFT_NAME(<Swift Name>.<Swift Name>) also works. But only one level in. So much for convenience.

The final attempt

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ilya Kulakov
Ilya Kulakov

Written by Ilya Kulakov

Tech Entrepreneur and Quality Freak with passion.

No responses yet

Write a response