How do I know whether the compiler has ARC support enabled?

后端 未结 3 1920
無奈伤痛
無奈伤痛 2020-12-05 19:55

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 releas         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 20:28

    You generally do not want to do things like this:

    #if ARC
       // do nothing
    #else
       [myObject release]
    #endif
    

    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!):

    #if __has_feature(objc_arc)
        #define MY_RELEASE(x) while (0) {}
    #else
        #define MY_RELEASE(x) [x release]
    #endif
    

    But I would be quite nervous to use even this, the pain potential is huge :)

提交回复
热议问题