如何以向后兼容的方式使用Objective-C __nonnull? [英] How to use Objective-C __nonnull in a backwards-compatible way?

查看:106
本文介绍了如何以向后兼容的方式使用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

这篇关于如何以向后兼容的方式使用Objective-C __nonnull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆