-->

How to use Objective-C __nonnull in a backwards-co

2019-04-10 06:06发布

问题:

Xcode has recently added __nonnull, __nullable, etc. attributes. However, they're not supported by older versions of clang and other compilers.

How can I use these attributes in a compatible way?

I hoped something like this would work:

#ifndef NS_ASSUME_NONNULL_BEGIN
#define __nonnull 
#endif

but it seems that NS_ASSUME_NONNULL_BEGIN is not a real macro, and it's "not defined" in Xcode7.

And it would make sense for this to work:

#if !defined(__is_identifier) || __is_identifier(__nonnull)
#define __nonnull
#define __nullable
#endif

but Xcode 6 chokes on that with "token is not a valid binary operator in a preprocessor subexpression" error.

回答1:

As explained in the question, all good methods seem not to work. The most blunt one does:

#if !defined(__clang_major__) || __clang_major__ < 7
#define __nonnull
#define __nullable
#endif