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
@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