ios - 用于了解 Xcode 4.3.2 中是否启用 ARC 的宏

标签 ios objective-c cocoa xcode4

在同一个文件中,我们要编写支持 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/

相关文章:

objective-c - .h 和 .m 文件中的@interface 定义之间的区别

swift - 如何检测选择了哪个 NSMenuItem

cocoa - 对 Cocoa 绘图应用程序的建议

xcode - Cocoa - 存储 NSString 值

ios - 在 UITextView 中的 attributeText 中使用 html 时删除控制字符(回车、格式化?等)

ios - 什么是 xcscheme 文件?它的目的是什么?

ios - 无法从今天的扩展程序中打开主机应用程序

java - Java SystemClock.uptimeMillis() 的 Objective C/Swift 版本

ios - Xcode在制作表单时将默认按钮设置为输入

objective-c - 在 UITabBarController 中重新排序 View Controller ,所有这些都在 Storyboard 中定义