如何以编程方式检测自动参考计数? [英] How to programmatically detect automatic reference counting?

查看:60
本文介绍了如何以编程方式检测自动参考计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我只是想知道.我希望我的代码以编程方式检测是否已启用ARC.我怎么做?有没有我可以检查的标志?实际的问题是,我已经编写了一个开源库.我已经在其中使用了releaseretain.如果其他人在启用ARC的情况下使用我的库文件,则我不希望它们出现任何错误.我该如何实现?还是有任何可能的方法可以提供任何工具来在使用库之前对其进行编译?

This might be a silly question, but I just wanted to know. I want my code to detect if ARC is enabled, programmatically. How do I do that? Is there is any flag that I could check? Actually the problem is that, I have written an open-source library. I have used release and retain in it. if some one else uses my library files using ARC enabled, I dont want them to get any errors. how do I achieve this? or is there any possible ways I can provide any tools to compile my library before using it?

推荐答案

#if !__has_feature(objc_arc)
    //Do manual memory management...
#else
    //Usually do nothing...
#endif

这当然是编译时检查,您不能在运行时检查ARC.

This is of course a compile-time check, you cannot check for ARC at runtime.

另一种选择是为在其他情况下使用ARC的项目中使用手动内存管理的文件设置-fno-objc-arc编译器标志.

An alternative would be to set the -fno-objc-arc compiler flag for your files that use manual memory management in a project that otherwise uses ARC.

您是否要为此烦恼,还是只在各处使用ARC,取决于您希望/需要的向后兼容性.同时支持ARC和MRC的代码很难阅读和维护.

Whether you want to bother with this at all or just use ARC everywhere depends on how backward-compatible you want/need to be. Code that supports both ARC and MRC can get quite hard to read and maintain.

这篇关于如何以编程方式检测自动参考计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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