如何以向后兼容的方式使用 Objective-C __nonnull?

How to use Objective-C __nonnull in a backwards-compatible way?

Xcode最近添加了__nonnull__nullable等属性。但是,旧版本的 clang 和其他编译器不支持它们。

如何以兼容的方式使用这些属性?

我希望这样的事情能奏效:

#ifndef NS_ASSUME_NONNULL_BEGIN
#define __nonnull 
#endif

不过NS_ASSUME_NONNULL_BEGIN好像不是真正的宏,在Xcode7.

中是"not defined"

这很有意义:

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

但是 Xcode 6 次阻塞,"token is not a valid binary operator in a preprocessor subexpression" 错误。

正如问题中所解释的,所有好的方法似乎都不起作用。最直率的是:

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