Friday, March 25, 2016

Surprises With Swift Extensions

Peter Steinberger:

These seemingly innocent extensions were overriding private API. Apple’s private API detection is not super sophisticated and wasn’t triggered when the app was uploaded to the App Store. It’s also not a public symbol so there were no warnings, not even a log message. Unprefixed categories are always dangerous, especially on classes that you do not own, like UIViewController. In PSPDFKit, we use categories for shared code, but prefix any method with pspdf_ to be absolutely sure we do not hit any name clashes. It’s certainly not pretty, and prefixes in Swift look even more alien, yet as you can see in this bug hunt, they are definitely necessary.

[…]

You can use @objc(prefix_name) to keep the name pretty in Swift and expose a prefixed version for the ObjC runtime.

Comments RSS · Twitter

Leave a Comment