Is it possible to enable non-arc files without add

2019-07-17 04:39发布

Is it possible to somehow handle the non-arc files without adding fno-objc-arc to the compile sources in the build phases? More specifically, is there any way to add fno-objc-arc somewhere in the code? The reason is, I want to open source one of my library which uses non-arc files and I don't want people who use my library to manually add fno-objc-arc. Just drag and drop...

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-17 05:09

No. But if you look at what some libraries do, they write macros that will conditionally call the MRC calls, e.g., release, autorelease, etc., depending upon whether the user is compiling with ARC or not, e.g. using the __has_feature(objc_arc) test. The code then uses these macros, rather than the standard release, retain, autorelease calls. With careful implementation, you can then have a single codebase support both ARC and MRC.

For example, look at FMDatabase.h of the FMDB library. Effectively, you replace your MRC calls with these macros, and they'll only be conditionally included, depending upon whether the project is using ARC or not.

#if ! __has_feature(objc_arc)
    #define FMDBAutorelease(__v) ([__v autorelease]);
    #define FMDBReturnAutoreleased FMDBAutorelease

    #define FMDBRetain(__v) ([__v retain]);
    #define FMDBReturnRetained FMDBRetain

    #define FMDBRelease(__v) ([__v release]);
#else
    // -fobjc-arc
    #define FMDBAutorelease(__v)
    #define FMDBReturnAutoreleased(__v) (__v)

    #define FMDBRetain(__v)
    #define FMDBReturnRetained(__v) (__v)

    #define FMDBRelease(__v)
#endif
查看更多
登录 后发表回答