objective-cswiftinteropobjective-c-nullability

How to Make Compiler Assume `nullable` by Default


Since Xcode 6.3, types in Objective-C can be marked with nullable or nonnull, here is Apple's blog post about this.

Problem is, when neither is specified, then the compiler imports the Objective-C code as implicitly unwrapped into Swift, e.g. NSView!. So when an object actually is nil, then it will crash when accessed from Swift. This does not produce a compiler error.

As this is extremely prone to fail, I'd like the compiler to assume everything from Objective-C by default as nullable, except when otherwise specified via nonnull, or the audited region macros NS_ASSUME_NONNULL_BEGIN / END. How can that be achieved?


Solution

  • Seems like there is no way around adding nullability annotations in order to get rid of implicitly unwrapped optionals.

    I wrote a script that finds all non-annotated headers which can also be added as a build phase, so no headers are overlooked.