标签:style blog io ar color sp strong on div
1.> __strong 和 __weak
Person.h
#import <Foundation/Foundation.h> @class Dog; @interface Person : NSObject { __strong Dog *_dog; //__weak Dog *_dog; } - (Dog *)dog; - (void)setDog:(Dog *)dog; @end
Dog.h
#import <Foundation/Foundation.h> @interface Dog : NSObject @end
main.m
#import <Foundation/Foundation.h> #import "Dog.h" #import "Person.h" int main(int argc, const char * argv[]) { Person *p = [[Person alloc] init]; Dog *dog = [[Dog alloc] init]; p.dog = dog; dog = nil; NSLog(@"%@",p.dog); return 0; }
如果是__weak 修饰的 打印结果为nil
如果是__strong 修饰的 打印结果正常
因为 __weak 是弱指针,在OC中没有强指针引用的对象会自动释放,当手动清空dog指针时候,__weak指针指向的dog对象就是去了唯一的强指针引用,会被释放。
__weak 相当于 @property(weak) __strong 相当于@property(strong)
标签:style blog io ar color sp strong on div
原文地址:http://www.cnblogs.com/dream-corner/p/4126441.html