在同一个文件中,我们要编写支持 ARC 和非 ARC 的代码。为此需要一些宏。
#ifdef ARC_ENABLED
NSLog(@" ARC enabled ");
#else
NSLog(@" ARC disabled ");
[self release];
#endif
如何实现这个宏,有没有什么宏可用? 请告诉我。先谢谢支持 注意:ARC_ENABLED 只是我写的例子
最佳答案
有一个 Objective-C 宏 __has_feature
,您可以使用它来检查是否启用了 arc。
来自 Clang Language Extension documentation
Automatic reference counting
Clang provides support for automated reference counting in Objective-C, which eliminates the need for manual retain/release/autorelease message sends. There are two feature macros associated with automatic reference counting:
__has_feature(objc_arc)
indicates the availability of automated reference counting in general, while__has_feature(objc_arc_weak)
indicates that automated reference counting also includes support for__weak
pointers to Objective-C objects.
本节Feature checking macro's是一本非常好的读物。
你可以像这样使用它..
#if !__has_feature(objc_arc)
//Do manual memory management...
#else
//Usually do nothing...
#endif
代码部分无耻地抄自this answer .
关于ios - 用于了解 Xcode 4.3.2 中是否启用 ARC 的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10930171/