關於ARC
ARC(Automatic Reference Counting)是一種據說可以自動釋放記憶體的方式,但帶來了不少弊端,例如:
·如果結構複雜一些的view,系統可能會釋放錯誤。
·由於記憶體是自動釋放,所以retain/release/autorelease/dealloc也不能使用了(也不需要使用了),如果使用,反而會報錯,如
ARC
forbids
explicit
message
send
of
release
或者
'release' is unavailable: not available in automatic reference counting mode..
顯然ARC和release是不能共存的,想要編譯通過就要幹掉一個。
通常被幹掉的會是ARC:
在老版的xcode中,開啟“Build Setting”,找到“Objective-C
Automatic Reference Counting”項,將它的值設定成“NO”
在較新的Xcode中如4.2,開啟“Build Setting”,找到"CLANG_ENABLE_OBJC_ARC"
將它的值設定成NO
關於strong和weak
強引用與弱引用的廣義區別:
強引用strong也就是我們通常所講的引用,其存亡直接決定了所指對象的存亡。如果不存在指向一個對象的引用,並且此對象不再顯示列表中,則此對象會被從記憶體中釋放。
弱引用weak除了不決定對象的存亡外,其他與強引用相同。即使一個對象被持有無數個若引用,只要沒有強引用指向他,那麽其還是會被清除。沒辦法,還是 “強哥”
有面子。
簡單地講,
在@property那裡用strong代替retain,用weak代替assign。
weak比assign多了一個功能,當對象消失後自動把指標變成nil,好處不言而喻。
關於@Autoreleasepool
NSAutoReleasePool 被 @ {Autoreleasepool / / Code for autoreleasepool } block
取代了。所起的作用還是一致的。
附上官方原文:
Automatic Reference Counting
Automatic Reference Counting (ARC) is a compiler-level feature that simplifies the process
of managing the lifetimes
of Objective-C objects. Instead
of you having to remember when to retain or
release an object,
ARC evaluates the lifetime requirements
of your objects and automatically inserts the appropriate method calls at compile time.
To be able to deliver these features,
ARC imposes some restricti*****—primarily enforcing some best practices and disallowing some other practices:
Do not call the retain,
release, autorelease, or dealloc methods in your code.
In addition, you cannot implement custom retain or
release methods.
Because you do not call the
release method, there is often no need to implement a custom dealloc method—the compiler synthesizes all that is required to relinquish ownership
of instance variables. You can provide a custom implementation
of dealloc if you need to manage other resources.
Do not store object pointers in C structures.
Store object pointers in other objects instead
of in structures.
Do not directly cast between object and nonobject types (for example, between id and void*).
You must use special functi***** or casts that tell the compiler about an object’s lifetime. You use these to cast between Objective-C objects and Core Foundation objects.
You cannot use NSAutoreleasePool objects.
Instead, you must use a new @autoreleasepool keyword to mark the start
of an autorelease block. The contents
of the block are enclosed by curly braces, as shown in the following example
@autoreleasepool
{
// Your code here
}
ARC encourages you to think in terms
of object graphs, and the relati*****hips between objects, rather than in terms
of retain and
release. For this reason,
ARC introduces new lifetime qualifiers for objects, including zeroing weak references. The value
of a zeroing weak reference is automatically set to nil if the object to which it points is deallocated. There are qualifiers for variables, and new weak and strong declared property
attributes, as illustrated in the following examples:
// The following declaration is a synonym for: @property(retain) MyClass *myObject;
@property(strong) MyClass *myObject;
// The following declaration is similar to "@property(assign) MyOtherClass *delegate;"
// except that if the MyOtherClass instance is deallocated,
// the property value is set to nil instead
of remaining as a dangling pointer
@property(weak) MyOtherClass *delegate;
Xcode provides migration tools to help convert existing projects to use
ARC. For more information about how to perform this migration, see Xcode New Features User Guide. For more information about
ARC itself, see Programming With
ARC
Release Notes.