问题描述
有没有办法让编译器询问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时的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!