Transitioning to ARC Release Notes

将苹果的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.
  1. not use NSAllocateObject,NSDeallocateObject,use alloc.
  2. not use OC object points in C structures,use oc class instead.
  3. not casual casting between id and void *.
    • __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.
  4. not use NSAutoReleasePoll,use @autoreleasepoll instead.
  5. no memory zones,no need to use NSZone any more.
  6. 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

  1. a weak reference not extend timelife of object it points to,and automatically becomes nil when there are no strong references to the object.
  2. use weak can refuse strong reference cycles (retain cycles).
  3. new property attribute. weak/strong. strong is default for object types.
  4. 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.
  5. decorate variables correctly: ClassName *qualifiers variableName;.
  6. _weak variables in stack.

Avoid Strong Reference Cycles

  1. 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.
  2. strong,weak, autoreleasing stack variables are initialized with nil,In ARC.
  3. instance variables is become strong.
  4. not use strong id In structures.
    a few possible solutions:
    • Objective-C instead struct
    • use void *
    • use unsafe_unretained
  5. -fno-objc-arc, - fobjc-arc.

你可能感兴趣的:(Transitioning to ARC Release Notes)