如何在课堂上要求 ARC?
我有一个同时包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下应该可行:
将其放在文件的顶部。
The following should work:
Place it at the top of your file.
如果它是一个完整的文件,我建议将 -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