Objective-C - ARC(Automatic Reference Counting)自動引用技術詳解

來源:互聯網
上載者:User

標籤:objective   arc   arc特點與判斷準則   自動引用技術   

ARC特點與判斷準則
/* ARC的判斷準則:只要沒有強指標指向對象,就會釋放對象 1.ARC特點 1> 不允許調用release、retain、retainCount 2> 允許重寫dealloc,但是不允許調用[super dealloc] 3> @property的參數  * strong :成員變數是強指標(適用於OC物件類型)  * weak :成員變數是弱指標(適用於OC物件類型)  * assign : 適用於非OC物件類型 4> 以前的retain改為用strong 指標分2種: 1> 強指標:預設情況下,所有的指標都是強指標 __strong 2> 弱指標:__weak */int main(){    Dog *d = [[Dog alloc] init];    Person *p = [[Person alloc] init];    p.dog = d;    d = nil;    NSLog(@"%@", p.dog);    return 0;}void test(){    // 錯誤寫法(沒有意義的寫法)    __weak Person *p = [[Person alloc] init];    NSLog(@"%@", p);    NSLog(@"------------");}
@class Dog;@interface Person : NSObject@property (nonatomic, strong) Dog *dog;@property (nonatomic, strong) NSString *name;@property (nonatomic, assign) int age;@end
@implementation Person- (void)dealloc{    NSLog(@"Person is dealloc");    // [super dealloc];}@end
@interface Dog : NSObject@end
@implementation Dog- (void)dealloc{    NSLog(@"Dog is dealloc");}@end
ARC循環參考問題
/** *   當兩端循環參考的時候,解決方案: 1> ARC 1端用strong,另1端用weak 2> 非ARC 1端用retain,另1端用assign */int main(){    Person *p = [[Person alloc] init];    Dog *d = [[Dog alloc] init];    p.dog = d;    d.person = p;    return 0;}
@class Dog;@interface Person : NSObject@property (nonatomic, strong) Dog *dog;@end
@implementation Person- (void)dealloc{    NSLog(@"Person--dealloc");}@end
@class Person;@interface Dog : NSObject@property (nonatomic, weak) Person *person;@end
@implementation Dog- (void)dealloc{    NSLog(@"Dog--dealloc");}@end

Objective-C - ARC(Automatic Reference Counting)自動引用技術詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.