ARC特点与判断准则
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");
}
@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