I need to write a lib in my iOS app.
The statement should be pre-process define as :
myObject ...
#if ARC
// do nothing
#else
[myObject release]
#endif
or run-time process as:
if (ARC) {
// do nothing
} else {
[myObject release];
}
How can I do?
Please help me! Thank you.
You can use
__has_feature, like so:If you want to also build with GCC (Apple’s GCC does not support ARC), you may also need the following to determine the compiler:
Update
Combined, they would take the general form:
Then just use
MON_IS_ARC_ENABLED_IN_THIS_TRANSLATIONin your sources or for further#defines.If a compiler you use adds support, you would have to add a case for that (and compiler errors would likely catch the error in this case, since it would likely forbid use of ref count ops).
Note that this has extra checks to demonstrate how one can (and should) avoid defining reserved identifiers (based on a conversation in the comments). It’s not exhaustive, but a demonstration. If you find yourself writing conditional
__has_featurechecks often, you may want to define a new macro for that to reduce and simplify definitions.