Strong是默認值,因爲它通常是您想要的,但使用ARC時,編譯器會分析對象的生命需要多長時間並在適當的時間釋放內存。例如:
- (void)someMethod
{
NSDate* date = [[NSDate alloc] init]; // date is __strong by default
NSLog(@"The date: %@", date); // date still contains the object created above
// Sometime before this point, the object date pointed to is released by the compiler
}
弱引用只保留對象,而它有一個或多個其他強引用。只要最後一個強引用被破壞,對象就會被編譯器釋放,並且運行時將弱對象引用(變量)更改爲nil
。這使得弱變量在本地範圍內幾乎無用,就像上面的例子一樣。例如:
- (void)someMethod
{
__weak NSDate* date = [[NSDate alloc] init]; // The date created is released before it's ever assigned to date
// because date is __weak and the newly created date has no
// other __strong references
NSLog(@"The date: %@", date); // This always prints (null) since date is __weak
}
看到一個強和弱變量在局部範圍內一起工作的一個例子(這將只有非常有限的實用性和這裏真的是隻顯示演示變量引用弱):
- (void)someMethod
{
NSDate* date = [[NSDate alloc] init]; // date stays around because it's __strong
__weak NSDate* weakDate = date;
// Here, the dates will be the same, the second pointer (the object) will be the same
// and will remain retained, and the first pointer (the object reference) will be different
NSLog(@"Date(%p/%p): %@", &date, date, date);
NSLog(@"Weak Date(%p/%p): %@", &weakDate, weakDate, weakDate);
// This breaks the strong link to the created object and the compiler will now
// free the memory. This will also make the runtime zero-out the weak variable
date = nil;
NSLog(@"Date: %@", date); // prints (null) as expected
NSLog(@"Weak Date: %@", weakDate); // also prints (null) since it was weak and there were no more strong references to the original object
}
只是一個問題,在第二個示例代碼中,您說:「這總是打印(空),因爲日期是__weak」,日期是零,但是有沒有內存泄漏?或者它已被釋放? – 2012-04-21 20:44:39
@RamyAlZuhouri沒有內存泄漏。編譯器立即釋放新分配的日期,並且甚至不打擾分配(在這種情況下)。 – 2012-04-22 05:23:03
NSLog(@「Weak Date:%@」,date);不應該是NSLog(@「Weak Date:%@」,weakDate); – NMunro 2013-07-18 19:16:47