Objective-C Automatic Reference Counting




1 objective-c

1.1 property declarations
1.2 zeroing weak references
1.3 converting to





objective-c

the following rules enforced compiler when arc turned on:



retain, release, retaincount, autorelease or dealloc cannot sent objects. instead, compiler inserts these messages @ compile time automatically, including [super dealloc] when dealloc overridden.



programs cannot cast directly between id , void *. includes casting between foundation objects , core foundation objects.


programs must use special casts, or calls special functions, tell compiler more information object s lifetime.



an autorelease pool can used allocate objects temporarily , retain them in memory until pool drained . without arc, nsautoreleasepool object can created purpose. arc uses @autoreleasepool blocks instead, encapsulate allocation of temporary objects , deallocates them when end of block reached.



programs cannot call functions nsallocateobject , nsdeallocateobject
programs cannot use object pointers in c structures (structs)
programs cannot use memory zones (nszone)
to cooperate non-arc code, programs must use no method or declared property (unless explicitly choosing different getter) starts copy.

property declarations

arc introduces new property declaration attributes, of replace old attributes.





zeroing weak references

zeroing weak references feature in objective-c arc automatically clears (sets nil) weak-reference local variables, instance variables, , declared properties before object being pointed starts deallocating. ensures pointer goes either valid object or nil, , avoids dangling pointers. prior introduction of feature, weak references referred references not retaining, not set nil when object pointed deallocated (equivalent unsafe_unretained in arc), possibly leading dangling pointer. programmer typically had ensure possible weak references object set nil manually when being deallocated. zeroing weak references obviates need this.


zeroing weak references indicated using declared property attribute weak or using variable attribute __weak.


zeroing weak references available in mac os x lion (10.7) or later , ios 5 or later, because require additional support objective-c runtime. however, os x classes not support weak references. code uses arc needs support versions of os older above cannot use zeroing weak references, , therefore must use unsafe_unretained weak references. there exists third-party library called plweakcompatibility [1] allows 1 use zeroing weak references on these older os versions.


converting to

xcode 4.2 or later provides way convert code arc. of xcode 4.5, found choosing edit > refactor > convert objective-c arc... although xcode automatically convert code, code may have converted manually. xcode inform developer when more complex use cases arise, such when variable declared inside autorelease pool , used outside or when 2 objects need toll-free bridged special casts.








Comments

Popular posts from this blog

History Shake Shack

David Aceveda List of The Shield characters

Prehistory History of Utah