Conditional compilation when using ARC

2020-03-17 03:43发布

问题:

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

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

回答1:

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

This is from the ARC docs from Clang.