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

--

--