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(@"------------");

}

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

 

@end

 

#import "Dog.h"

 

@implementation Dog

- (void)dealloc

{

    NSLog(@"Dog is dealloc");

}

@end

#import <Foundation/Foundation.h>

 

@class Dog;

 

@interface Person : NSObject

 

@property (nonatomic, strong) Dog *dog;

 

 

@property (nonatomic, strong) NSString *name;

 

@property (nonatomic, assign) int age;

 

@end

 

#import "Person.h"

 

@implementation Person

 

- (void)dealloc

{

    NSLog(@"Person is dealloc");

    

    // [super dealloc];

}

 

@end

 

posted @ 2016-03-28 11:51  lance.xiang  阅读(101)  评论(0)    收藏  举报