Core Foundation框架
轉載自:http://blog.csdn.net/weiwangchao_/article/details/7744972
Core Foundation框架?(CoreFoundation.framework) 是一組C語言接口,它們為iOS應用程序提供基本數據管理和服務功能。下面列舉該框架支持進行管理的數據以及可提供的服務:
-
群體數據類型 (數組、集合等)
-
程序包
-
字符串管理
-
日期和時間管理
-
原始數據塊管理
-
偏好管理
-
URL及數據流操作
-
線程和RunLoop
-
端口和soket通訊
Core Foundation框架和Foundation框架緊密相關,它們為相同功能提供接口,但Foundation框架提供Objective-C接口。如果您將Foundation對象和Core Foundation類型摻雜使用,則可利用兩個框架之間的 “toll-free bridging”。所謂的Toll-free bridging是說您可以在某個框架的方法或函數同時使用Core Foundatio和Foundation 框架中的某些類型。很多數據類型支持這一特性,其中包括群體和字符串數據類型。每個框架的類和類型描述都會對某個對象是否為 toll-free bridged,應和什么對象橋接進行說明。
如需進一步信息,請閱讀Core Foundation 框架參考。
自 Xcode4.2 開始導入ARC機制后,為了支持對象間的轉型,Apple又增加了許多轉型用的關鍵字。這一講我們就來了解其用法,以及產生的理由。
引子
我們先來看一下ARC無效的時候,我們寫id類型轉void*類型的寫法:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSObject</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> alloc</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">]</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> init</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> obj</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li></ol>反過來,當把void*對象變回id類型時,只是簡單地如下來寫,
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> p</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">obj release</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li></ol>但是上面的代碼在ARC有效時,就有了下面的錯誤:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> error</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">:</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">implicit</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> conversion of an </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Objective</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">-</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">C pointer</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> to </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">’</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">is</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> disallowed </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">with</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> ARC</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> obj</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">^</span></li><li class="L4" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L5" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> error</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">:</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">implicit</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> conversion of a non</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">-</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Objective</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">-</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">C pointer</span></li><li class="L6" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> type </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">’</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> to </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">is</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> disallowed </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">with</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> ARC</span></li><li class="L7" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> id o </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> p</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L8" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">^</span></li></ol>
__bridge
為了解決這一問題,我們使用?__bridge?關鍵字來實現id類型與void*類型的相互轉換。看下面的例子。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSObject</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> alloc</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">]</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> init</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">obj</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L4" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id o </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li></ol>將Objective-C的對象類型用 __bridge 轉換為 void* 類型和使用 __unsafe_unretained 關鍵字修飾的變量是一樣的。被代入對象的所有者需要明確對象生命周期的管理,不要出現異常訪問的問題。
除過 __bridge 以外,還有兩個 __bridge 相關的類型轉換關鍵字:
- __bridge_retained
- __bridge_transfer
接下來,我們將看看這兩個關鍵字的區別。
__bridge_retained
先來看使用 __bridge_retained 關鍵字的例子程序:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSObject</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> alloc</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">]</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> init</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_retained </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">obj</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li></ol>從名字上我們應該能理解其意義:類型被轉換時,其對象的所有權也將被變換后變量所持有。如果不是ARC代碼,類似下面的實現:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSObject</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> alloc</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">]</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> init</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> obj</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p retain</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li></ol>可以用一個實際的例子驗證,對象所有權是否被持有。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="lit" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 102, 102);">0</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSObject</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> alloc</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">]</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> init</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li><li class="L4" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> p </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_retained </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">obj</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L5" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">}</span></li><li class="L6" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L7" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSLog</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(@</span><span class="str" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"class=%@"</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">class</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">]);</span></li></ol>出了大括號的范圍后,p 仍然指向一個有效的實體。說明他擁有該對象的所有權,該對象沒有因為出其定義范圍而被銷毀。
__bridge_transfer
相反,當想把本來擁有對象所有權的變量,在類型轉換后,讓其釋放原先所有權的時候,需要使用 __bridge_transfer 關鍵字。文字有點繞口,我們還是來看一段代碼吧。
如果ARC無效的時候,我們可能需要寫下面的代碼。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="com" style="border: 0px; margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// p 變量原先持有對象的所有權</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">obj retain</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">[(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p release</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">];</span></li></ol>那么ARC有效后,我們可以用下面的代碼來替換:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="com" style="border: 0px; margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// p 變量原先持有對象的所有權</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id obj </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_transfer id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">p</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li></ol>可以看出來,__bridge_retained 是編譯器替我們做了 retain 操作,而 __bridge_transfer 是替我們做了 release1。
Toll-Free bridged
在iOS世界,主要有兩種對象:Objective-C 對象和 Core Foundation 對象0。Core Foundation 對象主要是有C語言實現的 Core Foundation Framework 的對象,其中也有對象引用計數的概念,只是不是 Cocoa Framework::Foundation Framework 的 retain/release,而是自身的 CFRetain/CFRelease 接口。
這兩種對象間可以互相轉換和操作,不使用ARC的時候,單純的用C原因的類型轉換,不需要消耗CPU的資源,所以叫做 Toll-Free bridged。比如 NSArray和CFArrayRef, NSString和CFStringRef,他們雖然屬于不同的 Framework,但是具有相同的對象結構,所以可以用標準C的類型轉換。
比如不使用ARC時,我們用下面的代碼:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> stringWithFormat</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">:...];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> cfString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">;</span></li></ol>同樣,Core Foundation類型向Objective-C類型轉換時,也是簡單地用標準C的類型轉換即可。
但是在ARC有效的情況下,將出現類似下面的編譯錯誤:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">Cast</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> of </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">Objective</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">-</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">C pointer type </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">‘</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> to C pointer type </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">‘</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">aka </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">‘</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">const</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">struct</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> __CFString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*’)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> requires a bridged cast</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">Use</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> __bridge to convert directly </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">no</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> change </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">in</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> ownership</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">Use</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> __bridge_retained to make an ARC </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">object</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> available </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">as</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> a </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">+</span><span class="lit" style="border: 0px; margin: 0px; padding: 0px;">1</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">‘</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">’</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">aka </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">‘</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">const</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">struct</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> __CFString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*’)</span></li></ol>錯誤中已經提示了我們需要怎樣做:用 __bridge 或者 __bridge_retained 來轉型,其差別就是變更對象的所有權。
正因為Objective-C是ARC管理的對象,而Core Foundation不是ARC管理的對象,所以才要特意這樣轉換,這與id類型向void*轉換是一個概念。也就是說,當這兩種類型(有ARC管理,沒有ARC管理)在轉換時,需要告訴編譯器怎樣處理對象的所有權。
上面的例子,使用 __bridge/__bridge_retained 后的代碼如下:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> stringWithFormat</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">:...];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> cfString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">;</span></li></ol>只是單純地執行了類型轉換,沒有進行所有權的轉移,也就是說,當string對象被釋放的時候,cfString也不能被使用了。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> stringWithFormat</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">:...];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> cfString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_retained </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">;</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">...</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFRelease</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">cfString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="com" style="border: 0px; margin: 0px; padding: 0px;">// 由于Core Foundation的對象不屬于ARC的管理范疇,所以需要自己release</span></li></ol>使用 __bridge_retained 可以通過轉換目標處(cfString)的 retain 處理,來使所有權轉移。即使 string 變量被釋放,cfString 還是可以使用具體的對象。只是有一點,由于Core Foundation的對象不屬于ARC的管理范疇,所以需要自己release。
實際上,Core Foundation 內部,為了實現Core Foundation對象類型與Objective-C對象類型的相互轉換,提供了下面的函數。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFTypeRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFBridgingRetain</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id X</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">{</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">return</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_retained </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFTypeRef</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">X</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">;</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">}</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L4" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">id </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFBridgingRelease</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFTypeRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> X</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">{</span></li><li class="L5" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">return</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_transfer id</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">X</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">;</span></li><li class="L6" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">}</span></li></ol>所以,可以用 CFBridgingRetain 替代 __bridge_retained 關鍵字:
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">*</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">[</span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> stringWithFormat</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">:...];</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFStringRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> cfString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFBridgingRetain</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px;">string</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">);</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">...</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px;">CFRelease</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">cfString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px;">);</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="com" style="border: 0px; margin: 0px; padding: 0px;">// 由于Core Foundation不在ARC管理范圍內,所以需要主動release。</span></li></ol> __bridge_transfer所有權被轉移的同時,被轉換變量將失去對象的所有權。當Core Foundation對象類型向Objective-C對象類型轉換的時候,會經常用到 __bridge_transfer 關鍵字。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">CFStringRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> cfString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">CFStringCreate</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">...();</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">string</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">__bridge_transfer </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*)</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">cfString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span></li><li class="L2" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">?</span></li><li class="L3" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="com" style="border: 0px; margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// CFRelease(cfString); 因為已經用 __bridge_transfer 轉移了對象的所有權,所以不需要調用 release</span></li></ol>同樣,我們可以使用 CFBridgingRelease() 來代替 __bridge_transfer 關鍵字。
<ol class="linenums" style="border: 0px; margin: 0px; padding: 0px 0px 0px 26px;"><li value="1" class="L0" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal;"><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">CFStringRef</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> cfString </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">CFStringCreate</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">...();</span></li><li class="L1" style="border: 0px; margin: 0px 0px 0px 20px; padding: 0px; list-style-type: decimal; background-color: rgb(238, 238, 238);"><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="kwd" style="border: 0px; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">string</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">CFBridgingRelease</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="border: 0px; margin: 0px; padding: 0px;">cfString</span><span class="pun" style="border: 0px; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span></li></ol>總結
由上面的學習我們了解到 ARC 中類型轉換的用法,那么我們實際使用中按照怎樣的原則或者方法來區分使用呢,下面我總結了幾點關鍵要素。
- 明確被轉換類型是否是 ARC 管理的對象
- Core Foundation 對象類型不在 ARC 管理范疇內
- Cocoa Framework::Foundation 對象類型(即一般使用到的Objectie-C對象類型)在 ARC 的管理范疇內
- 如果不在 ARC 管理范疇內的對象,那么要清楚 release 的責任應該是誰
- 各種對象的生命周期是怎樣的
1.?聲明 id obj 的時候,其實是缺省的申明了一個 __strong 修飾的變量,所以編譯器自動地加入了 retain 的處理,所以說 __bridge_transfer 關鍵字只為我們做了 release 處理。
總結
以上是生活随笔為你收集整理的Core Foundation框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 08-CoreData学习总结
- 下一篇: IOS用CGContextRef画各种图