如何在类中需要ARC? [英] How to require ARC in a class?

查看:153
本文介绍了如何在类中需要ARC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序与ARC代码和非ARC代码。当我尝试将非ARC代码编译为ARC时,编译器将捕获。当我的ARC代码被错误地编译没有ARC时,如何导致编译时错误/通知?显然,代码将编译。它只会泄漏。静态分析器将捕获问题。我宁愿找到一种方式在我的ARC代码中留下pragma或定义。

I have an app with both ARC code and non-ARC code. The compiler will catch when I try to compile non-ARC code as ARC. How do I cause a compile time error/notice when my ARC code is erroneously compiled without ARC? Obviously, the code will compile. It will just leak. The static analyzer will catch the problem. I would rather find a way to leave a pragma or define in my ARC code.

以下是由Apple在 objc-api.h中定义的

The following is defined by Apple in objc-api.h:

/* OBJC_ARC_UNAVAILABLE: unavailable with -fobjc-arc */
#if !defined(OBJC_ARC_UNAVAILABLE)
#   if __has_feature(objc_arr)
#       define OBJC_ARC_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode")))
#   else
#       define OBJC_ARC_UNAVAILABLE
#   endif
#endif

我的C宏弱。我该如何使用它?或者,也许有一个更好的符号来检查?

My C-macro-fu is weak. How would I use it? Or, perhaps there is a better symbol to check?

我问,因为我构建我的应用程序从可重用的库。

P.S. I ask because I build much of my app from reusable libraries. I want to ensure that each file is compiled in the right way.

推荐答案

以下内容应该可以正常工作:

The following should work:

#if !__has_feature(objc_arc)
#  error Compile me with ARC, please!
#endif

将它放在文件的顶部。

这篇关于如何在类中需要ARC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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