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

非 Y 不嫁゛ 提交于 2019-12-06 04:17:01

问题


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


来源:https://stackoverflow.com/questions/31105433/how-to-use-objective-c-nonnull-in-a-backwards-compatible-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!