使用ARC时的条件编译 [英] Conditional compilation when using ARC

查看:178
本文介绍了使用ARC时的条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让编译器询问ARC是否已打开,然后根据该值有条件地编译?例如,我有一个协议:

Is there a way to ask the compiler if ARC is turned on, and then conditionally compile based upon that value? For example, I have a protocol:

@protocol ProtocolA

@required
-(void)protocolMethodOne

@optional
-(void)protocolMethodTwo;

@end

如果我使用ARC,我想make protocolMethodA 使用ARC时是可选的,不使用ARC时需要。这是因为使用这种方法的主要原因之一是取消分配对象实例。

If I'm using ARC, I would like to make protocolMethodA optional when using ARC, and required when not using ARC. This is because one of the main reasons for utilizing this method is to dealloc the object instance.

据说,这就是我想要发生的事情:

With that said, here's what I would like to happen:

@protocol ProtocolA

#ifdef SOME_ARC_VARIABLE
    @optional
#else
    @required
#endif
-(void)protocolMethodOne

@optional
-(void)protocolMethodTwo;

@end


推荐答案

你应该 #if __ has_feature(objc_arc)在启用ARC的情况下,这将扩展为1.

You should do #if __has_feature(objc_arc) That will expand to 1 in the case of ARC being enabled.

这来自Clang的 ARC文档

这篇关于使用ARC时的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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