since: 2011/11/12
update: 2011/11/12
references:
Beginning ARC in iOS 5 Tutorial Part 1 | Ray Wenderlich
1. 當在專案中使用 ARC 時, 將不再使用 retain, release 和 autorelease;
因為啟用 ARC 功能後, 編譯器將會為你處理這些問題.
2. "strong" 指標: 可保持讓物件存活; 預設所有的 instance variables 與 local variables
都是 strong pointers.
3. "weak" 指標: 仍然會指向物件, 但是沒有擁有權.
a. 當 weak pointer 指向的物件被釋放後, weak pointer 本身變成 nil, 稱為:
"zeroing" weak pointer; 因而不會產生 "dangling pointers" 或 "zombies" 的問題.
b. weak pointers 的常用時機: 當二個物件處於 parent-child 關係.
=> parent 物件有一個 strong pointer 指向 child 物件, 因此擁有 child 物件;
但是為了防止擁有權的無盡循環, child 物件只有一個 weak pointer 指回
parent 物件.
ex: delegate pattern
(1). view controller: may own a UITableView through a strong pointer.
(2). The table view's data source and delegate pointers point back
at the view controller, but are weak.
4. assigning retained object to weak variable;
object will be released after assignment.
ex:
__weak NSString *str = [[NSString alloc] initWithFormat:...];
NSLog(@"%@", str); // will output "(null)"
5. ARC only works on Objective-C objects. If your app uses Core Foundation
or malloc() and free(), then you're still responsible for doing the memory
management there.
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。