Transitioning to ARC Release Not
2017-12-05 本文已影响0人
涛_3c6a
将苹果的Transitioning to ARC Release Notes内容,简单整理一下,作为快速查询手册。
ARC Enforces New Rules
- not invoke dealloc.
- not implement and invoke
release
,retain
,autorelease
,retainCount
. - can implement dealloc method,but not call
[super dealloc]
. - can use
CFRetain
,CFRelease
,other Core-Foundation style objects.
- not use
NSAllocateObject
,NSDeallocateObject
,usealloc
. - not use OC object points in C structures,use oc class instead.
- not casual casting between
id
andvoid *
.-
__bridge
. transfer between OC and CF,ownership not transfer. -
__bridge_retained
,CFBridgeingRetain
. cast OC to CF,ownership to you. -
__bridge_transfer
,CFBridgeingRelease
. cast CF to OC,ownership to ARC.
-
- not use
NSAutoReleasePoll
,use@autoreleasepoll
instead. - no memory zones,no need to use
NSZone
any more. - an accessor's name not begin with new,unless specify a different getter.
- @property NSString *newTitle. //not work.
- @property (getter = theNewTitle) NSString *newTitle. //work
New Lifetime Qualifiers
- a weak reference not extend timelife of object it points to,and automatically becomes
nil
when there are no strong references to the object. - use weak can refuse strong reference cycles (retain cycles).
- new property attribute.
weak
/strong
.strong
is default for object types. - lifetime qualifiers of variables.
- __strong. default. An object remains "alive" as long as there is a strong pointer to it.
- __weak. not keep the reference of object alive,and when no strong reference point to ,set nil.
- __unsafe_unretained. similar to weak,but not set to nil.
- __autoreleasing. used to denote arguments that passed by reference(id *), and are autoreleased on return.
- decorate variables correctly:
ClassName *qualifiers variableName;
. - _weak variables in stack.
Avoid Strong Reference Cycles
- strong reference cycles.
- __block
- In MRC, __block id x, not retain x.
- In ARC, __block id x, retain x.
so, in ARC,use _weak or set _block value to nil.
__ block作用:在block代码块中是否可改变block外变量的值.
The best option is use __weak.
- __block
- strong,weak, autoreleasing stack variables are initialized with nil,In ARC.
- instance variables is become strong.
- not use strong id In structures.
a few possible solutions:- Objective-C instead struct
- use
void *
- use
unsafe_unretained
- -fno-objc-arc, - fobjc-arc.