怎样取消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 withmalloc()
, - 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
toNSObject
you useCFBridgingRelease()
. - When changing ownership from
NSObject
toCore Foundation
you useCFBridgingRetain()
. - When you want to use one type temporarily as if it were another without ownership change, you use
__bridge
(这个我也不是很明白,原文中有例子,但是没太懂).
网友评论