Skip to content

Instantly share code, notes, and snippets.

@steipete
Created February 25, 2015 23:22
Show Gist options
  • Save steipete/d9f519858fe5fb5533eb to your computer and use it in GitHub Desktop.
Save steipete/d9f519858fe5fb5533eb to your computer and use it in GitHub Desktop.
Want nullability right away? This will degrade gracefully until you can drop Xcode 6.1/Xcode 6.2.
// Xcode 6.3 defines new language features to declare nullability
#if __has_feature(nullability)
#define PSPDF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#define PSPDF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
#define ps_nullable nullable
#define ps_nonnull nonnull
#define ps_null_unspecified null_unspecified
#define ps_null_resettable null_resettable
#define __ps_nullable __nullable
#define __ps_nonnull __nonnull
#define __ps_null_unspecified __null_unspecified
#else
#define PSPDF_ASSUME_NONNULL_BEGIN
#define PSPDF_ASSUME_NONNULL_END
#define ps_nullable
#define ps_nonnull
#define ps_null_unspecified
#define ps_null_resettable
#define __ps_nullable
#define __ps_nonnull
#define __ps_null_unspecified
#endif
@steipete
Copy link
Author

weak delegates are not yet automatically inferred nullable in Xcode 6.3b2; but this should be fixed soon: https://twitter.com/jckarter/status/570725579068084224

The issue with NSError ** has been fixed in Beta 2: https://twitter.com/jckarter/status/564953656139579392

@regexident
Copy link

This will degrade gracefully until you can drop Xcode 6.1/Xcode 6.2.

…as would this, no?

#if !__has_feature(nullability)
#define NS_ASSUME_NONNULL_BEGIN
#define NS_ASSUME_NONNULL_END
#define nullable
#define nonnull
#define null_unspecified
#define null_resettable
#define __nullable
#define __nonnull
#define __null_unspecified
#endif

Any particular reason for using a prefix here? I'm curious.

@steipete
Copy link
Author

@regexident Ha. That's very smart, I'll try that out right away.

Xcode 6.3b3 also added NS_ASSUME_NONNULL_BEGIN to further simplify this.

@regexident
Copy link

😉

@maniak-dobrii
Copy link

Prefix-less defines could cause issues with some __attribute__ attributes, for example with:

...
#define nonnull
...
- (void)doSomethingWith:(id)something  __attribute__((nonnull()));

Last line will produce error. So, existing codebases must adopt new nullability features or use something old like __attribute__((nonnull())). Interesting, how will they interact if some argument will be specified as nullable while having __attribute__((nonnull())).

@samjarman
Copy link

In xcode 7, I get Macro name is a reserved identifier with the macros starting with __. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment