Conditional compilation when using ARC

2020-03-17 03:52发布

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条回答
聊天终结者
2楼-- · 2020-03-17 04:44

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.

查看更多
登录 后发表回答