参考文章:Library in Objective C for OSX
摘录原文:
Compiled MRC/ARC code can be called from ARC/MRC code - ARC is essentially a compile-time technology which automatically inserts calls to the reference-counting memory management routines. At runtime ARC & MRC code interoperates without issue.
Therefore if you intend to ship your library in compiled form you can write it using either MRC or ARC and it can be used by both ARC & MRC projects.
If you intend to ship your library in source form then you can [was "must", see edit below] write it using MRC. Users can then incorporate your library code directly into MRC projects.
To include it in ARC projects users add the source as usual and then for each source file the file must be marked as using MRC. This is done by selecting the file in the Compiler Sources section of the Build Phases tab of the Project settings and adding the flag -fno-objc-arc. The flag instructs the compiler to compile that particular source file as MRC.
Edit
The above was over strong, in general with ARC being newer than MRC then requirement is to include MRC source in ARC projects, and this is done by flagging the individual MRC files with -fno-objc-arc. However the reverse is equally supported, you can include ARC source in an MRC project by flagging the individual ARC files with -fobjc-arc.
In other words, Xcode has a project-wide setting, "Objective-C Automatic Reference Counting" found in the "Build Settings" pane of the project settings, which sets whether the project is MRC/ARC; and this setting can be reversed for individual source files, in the "Compile Sources" section of the "Build Phases" pane of project settings.
The simplest choice is to distribute your library in binary (compiled) form, as this avoids the user setting any flags.
解释一下:
我们都知道,如果在ARC下用MRC的类文件(.m),我们需要在targets
的build phases
选项Compile Sources
下选择要不使用arc编译的文件,双击它,输入 -fno-objc-arc
即可,反之,类似,输入 -fobjc-arc
即可,那静态库文件(.a)需要这样处理吗?
答案是:不用的!
静态库在ARC和MRC下都不需要您来操心内存管理的事情,不需要另外添加flag值!原因是,静态库是二进制文件,在制作编译静态库的时候,如果作者是在ARC下,那么已经把对象释放的操作编译进去了,如果是MRC,那么已经内存管理了。