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

后端 未结 3 1926
無奈伤痛
無奈伤痛 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:42

    You can do it using macros:

    #if !defined(__clang__) || __clang_major__ < 3
        #ifndef __bridge
            #define __bridge
        #endif
    
        #ifndef __bridge_retain
            #define __bridge_retain
        #endif
    
        #ifndef __bridge_retained
            #define __bridge_retained
        #endif
    
        #ifndef __autoreleasing
            #define __autoreleasing
        #endif
    
        #ifndef __strong
            #define __strong
        #endif
    
        #ifndef __unsafe_unretained
            #define __unsafe_unretained
        #endif
    
        #ifndef __weak
            #define __weak
        #endif
    #endif
    
    #if __has_feature(objc_arc)
        #define SAFE_ARC_PROP_RETAIN strong
        #define SAFE_ARC_RETAIN(x) (x)
        #define SAFE_ARC_RELEASE(x)
        #define SAFE_ARC_AUTORELEASE(x) (x)
        #define SAFE_ARC_BLOCK_COPY(x) (x)
        #define SAFE_ARC_BLOCK_RELEASE(x)
        #define SAFE_ARC_SUPER_DEALLOC()
        #define SAFE_ARC_AUTORELEASE_POOL_START() @autoreleasepool {
        #define SAFE_ARC_AUTORELEASE_POOL_END() }
    #else
        #define SAFE_ARC_PROP_RETAIN retain
        #define SAFE_ARC_RETAIN(x) ([(x) retain])
        #define SAFE_ARC_RELEASE(x) ([(x) release])
        #define SAFE_ARC_AUTORELEASE(x) ([(x) autorelease])
        #define SAFE_ARC_BLOCK_COPY(x) (Block_copy(x))
        #define SAFE_ARC_BLOCK_RELEASE(x) (Block_release(x))
        #define SAFE_ARC_SUPER_DEALLOC() ([super dealloc])
        #define SAFE_ARC_AUTORELEASE_POOL_START() NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        #define SAFE_ARC_AUTORELEASE_POOL_END() [pool release];
    #endif
    

    The above came from the site: http://raptureinvenice.com/arc-support-without-branches/; but I've pasted it to ensure it's not lost.

提交回复
热议问题