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.


以下内容原文出自这里

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相关的函数

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 (这个我也不是很明白,原文中有例子,但是没太懂).
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,552评论 5 6
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,941评论 0 23
  • 看了这部电影有一段时间了,一直都想好好的写点感想。我不是一个煽情的人,但是我看这部电影竟然颇有感触,导演达米恩•查...
    EHNY阅读 581评论 0 0
  • 一个项目涉及多种角色 外部: 购买产品的客户(甲方) 医信区域项目方(打单、沟通甲方) HIS技术人员(医院现场人...
    666上顺阅读 593评论 0 0
  • 最近正在看两本关于房地产的书,这两本书的观点截然相反,弄的我也很迷惑。我只看了不到三分之一,仍然在思考其中的道理与...
    zhk1991阅读 252评论 0 0