【概念釋疑】iOS中的靜態(tài)庫需要內(nèi)存管理嗎?

參考文章: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),我們需要在targetsbuild 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)存管理了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容