如何在课堂上要求 ARC?

发布于 2024-12-19 12:16:03 字数 641 浏览 1 评论 0原文

我有一个同时包含 ARC 代码和非 ARC 代码的应用程序。当我尝试将非 ARC 代码编译为 ARC 时,编译器会捕获。当我的 ARC 代码在没有 ARC 的情况下错误编译时,如何导致编译时错误/通知?显然,代码可以编译。它只会泄漏。静态分析器会发现问题。我宁愿找到一种方法在我的 ARC 代码中留下编译指示或定义。

以下是 Apple 在 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-macro-fu 很弱。我该如何使用它?或者,也许有更好的符号需要检查?

PS 我问这个问题是因为我的大部分应用程序都是从可重用库构建的。我想确保每个文件都以正确的方式编译。

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撞了怀 2024-12-26 12:16:03

以下应该可行:

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

将其放在文件的顶部。

The following should work:

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

Place it at the top of your file.

无需解释 2024-12-26 12:16:03

如果它是一个完整的文件,我建议将 -fno-objc-arc 添加到编译器标志以使用非弧编译器。

使用宏通过非弧编译器编译代码的“部分”。
例如,如果您正在编写一个将在 arc 和非 arc 代码库中使用的框架,并在此宏块中编写释放、释放,请使用此宏

If it is a complete file, I would recommend adding -fno-objc-arc to the compiler flags to use the non-arc compiler.

Use the macro to compile "parts" of code with the non-arc compiler.
for example, use this if you are writing a framework that will be used in both arc and non-arc code bases and write release, deallocs within this macro block

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文