如何以向后兼容的方式使用Objective-C __nonnull?
Xcode最近添加了__nonnull
,__nullable
等属性.但是,较旧版本的clang和其他编译器不支持它们.
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
,但是NS_ASSUME_NONNULL_BEGIN
似乎不是真正的宏,并且在Xcode7中是未定义".
but it seems that NS_ASSUME_NONNULL_BEGIN
is not a real macro, and it's "not defined" in Xcode7.
这很有意义:
#if !defined(__is_identifier) || __is_identifier(__nonnull)
#define __nonnull
#define __nullable
#endif
但是Xcode 6却因令牌在预处理器子表达式中不是有效的二进制运算符"错误而窒息.
but Xcode 6 chokes on that with "token is not a valid binary operator in a preprocessor subexpression" error.
如问题中所述,所有好的方法似乎都不起作用.最直率的人做到了:
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