Subclassing Swift class in Obj-c [Private-API Friday #1]


Let’s create our guinea pig class.

Xcode checks for the attribute and macro existence and defines the new macro.


Yes! Private API! Let’s trick Xcode by defining our own “SWIFT_CLASS.” I forget how to do this locally and will change GCC preferences instead. Let me know if you can make this locally.


Objc is not a Swift. No, really, it’s very different languages under the hood. And with an easy bridging provided by Xcode this can be easily forgotten. I will add two simple functions to our example class:

  • It’s a private API. So all of its problems are applicable here (stability, UB, etc).
  • Cycle bridging headers. You can’t use your subclass back in Swift, because Swift files should be compiled before your subclass, but they are using your class, so ¯\_(ツ)_/¯


Some rules and limitations exist for a reason. However, they shouldn’t be accepted just by the sole authority. Investigation of private APIs can be useful for better understanding system behavior and languages. It also can help to become a better public API developer. So I will start this series to share my insights from different hidden elements that I encounter.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


MacOS and iOS dev. ObjC paradigm fan.