參考文章: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選項(xiàng)Compile Sources下選擇要不使用arc編譯的文件,雙擊它,輸入 -fno-objc-arc 即可,反之,類似,輸入 -fobjc-arc 即可,那靜態(tài)庫文件(.a)需要這樣處理嗎?
答案是:不用的!
靜態(tài)庫在ARC和MRC下都不需要您來操心內(nèi)存管理的事情,不需要另外添加flag值!原因是,靜態(tài)庫是二進(jìn)制文件,在制作編譯靜態(tài)庫的時(shí)候,如果作者是在ARC下,那么已經(jīng)把對(duì)象釋放的操作編譯進(jìn)去了,如果是MRC,那么已經(jīng)內(nèi)存管理了。