Objc中的ARC

怎樣取消ARC

原文出自StackOverflow

It is possible to disable ARC for individual files by adding the -fno-objc-arc
compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.


以下內(nèi)容原文出自這里

When to use -dealloc() in ARC?

The only reason for keeping a dealloc() method around is when you need to free certain resources that do not fall under ARC’s umbrella. Examples of this are:

  • calling CFRelease() on Core Foundation objects,
  • calling free() on memory that you allocated with malloc(),
  • unregistering for notifications,
  • invalidating a timer, and so on.

When to use @property?

weak is the recommended relationship for all outlet properties. These view objects are already part of the view controller’s view hierarchy and don’t need to be retained elsewhere. The big advantage of declaring your outlets weak is that it saves you time writing the viewDidUnload method (in non-arc).

Using properties just for the purposes of simplifying memory management is no longer necessary.
You can still do so if you want to but think it’s better to just use instance variables now, and only use properties when you need to to make data accessible to other classes from your public interface.

As a best practice, if you define something as a property, then you should always use it as a property. The only places where you should access the property’s backing instance variable directly are in init
and when you provide a custom getter and setter. Anywhere else you should access the property through self.propertyName.

Bridging casts - 和bridging相關(guān)的函數(shù)

Now that we have ARC, the compiler needs to know who is responsible for releasing such casted objects.

If you treat an NSObject as a Core Foundation object, then it is no longer ARC’s responsibility to release it. But you do need to tell ARC about your intentions, the compiler cannot infer this by itself.

Likewise, if you create a Core Foundation object but then cast it to an NSObject, you need to tell ARC to take ownership of it and delete that object when its time comes. That’s what the bridging casts are for.

Anywhere you call a Core Foundation function named Create, Copy, or Retain you must do CFBridgingRelease() to safely transfer the value to ARC.

To summarize:

  • When changing ownership from Core Foundation to NSObject you use CFBridgingRelease().
  • When changing ownership from NSObject to Core Foundation you use CFBridgingRetain().
  • When you want to use one type temporarily as if it were another without ownership change, you use __bridge (這個(gè)我也不是很明白,原文中有例子,但是沒(méi)太懂).
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,552評(píng)論 5 6
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,941評(píng)論 0 23
  • 看了這部電影有一段時(shí)間了,一直都想好好的寫(xiě)點(diǎn)感想。我不是一個(gè)煽情的人,但是我看這部電影竟然頗有感觸,導(dǎo)演達(dá)米恩?查...
    EHNY閱讀 581評(píng)論 0 0
  • 一個(gè)項(xiàng)目涉及多種角色 外部: 購(gòu)買(mǎi)產(chǎn)品的客戶(甲方) 醫(yī)信區(qū)域項(xiàng)目方(打單、溝通甲方) HIS技術(shù)人員(醫(yī)院現(xiàn)場(chǎng)人...
    666上順閱讀 593評(píng)論 0 0
  • 最近正在看兩本關(guān)于房地產(chǎn)的書(shū),這兩本書(shū)的觀點(diǎn)截然相反,弄的我也很迷惑。我只看了不到三分之一,仍然在思考其中的道理與...
    zhk1991閱讀 252評(píng)論 0 0