Conditional compilation when using ARC
Asked Answered
E

1

7

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
Enamour answered 9/12, 2011 at 14:41 Comment(0)
T
14

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.

Towill answered 9/12, 2011 at 14:44 Comment(1)
Awesome. Looked all over for this and couldn't find it. Works great!Enamour

© 2022 - 2024 — McMap. All rights reserved.