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 do it using macros:
The above came from the site: http://raptureinvenice.com/arc-support-without-branches/; but I've pasted it to ensure it's not lost.
You generally do not want to do things like this:
Because it’s a recipe for disaster, there are many subtle bugs lurking in such code. But if you do have a sane use case for that, you would perhaps be better off with a macro (I didn’t know
__has_feature
, thanks Justin!):But I would be quite nervous to use even this, the pain potential is huge :)
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_TRANSLATION
in your sources or for further#define
s.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_feature
checks often, you may want to define a new macro for that to reduce and simplify definitions.