1 // 2 // main.m 3 // 03-arc-循环引用 4 // 5 // Created by apple on 13-8-11. 6 // Copyright (c) 2013年 itcast. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @class Dog; 12 13 @interface Person : NSObject 14 15 @property (nonatomic, strong) Dog *dog; 16 17 @end 18 19 #import "Person.h" 20 21 @implementation Person 22 23 - (void)dealloc 24 { 25 NSLog(@"Person--dealloc"); 26 } 27 28 @end 29 30 31 #import <Foundation/Foundation.h> 32 33 @class Person; 34 35 @interface Dog : NSObject 36 37 @property (nonatomic, weak) Person *person; 38 39 @end 40 41 #import "Dog.h" 42 43 @implementation Dog 44 - (void)dealloc 45 { 46 NSLog(@"Dog--dealloc"); 47 } 48 @end 49 50 51 52 #import <Foundation/Foundation.h> 53 #import "Person.h" 54 #import "Dog.h" 55 56 /* 57 当两端循环引用的时候,解决方案: 58 1> ARC 59 1端用strong,另1端用weak 60 61 2> 非ARC 62 1端用retain,另1端用assign 63 */ 64 65 int main() 66 { 67 Person *p = [[Person alloc] init]; 68 69 70 Dog *d = [[Dog alloc] init]; 71 p.dog = d; 72 d.person = p; 73 74 return 0; 75 }