strong修饰的对象: 指针指向对象时内存计数器会+1,只要有指针指向该对象,它的内存就不会被释放。
Sample: @property (nonatomic, strong) NSString *obj1;
@property (nonatomic, strong) NSString *obj2;
obj1 = @"object1"; //obj1指向对象object1, count = 1
obj2 = obj1; //obj2也指向对象object1, count = 2
obj1 = nil; // count = 1
NSLog(@"obj2 = %@",obj2);
Result: obj2 = object1
Analyze: obj1和obj2同时指向堆中的同一个内存对象, obj1=nil, 但是obj2还是指向对象"object1", 所以堆中的对象"object1"还存在,没有被释放。
weak修饰的对象:指针指向对象时内存计数器不会+1,如果没有strong修饰的指针指向该对象时,它的内存就会被释放。
Sample: @property (nonatomic, strong) NSString *obj1;
@property (nonatomic, weak) NSString *obj2;
obj1 = @"object1"; //obj1指向对象object1, count = 1
obj2 = obj1; //obj2也指向对象object1, 但是内存计数器不变,所以 count = 1
obj1 = nil; // count = 0
NSLog(@"obj2 = %@",obj2);
Result: obj2 = null
Analyze: obj1和obj2同时指向堆中的同一个内存对象, obj1=nil, 说明没有strong修饰的指针指向object1,所以内存被释放。
补充(according to Stanford iOS7 course):
strong means: keep the object that this property points to in memory until set this property to nil (zero)
(and it will stay in memory until everyone who has a strong pointer to it sets their property to nil too)
weak means: if no one else has a strong pointer to this object, then you can throw it out of memory and set this property to nil.
(this can happen at any time)