我还没有理解弱小和强烈的参考

时间:2012-04-19 21:46:28

标签: objective-c

通过书籍首先解释了关于“无效”引用的问题(使用ARC),如下例所示:

NSDate* date1=[[NSDate alloc]init];
NSDate* date2=date1;
[date1 release];
NSLog(@"%@",date2); // bad access

所以我理解了保留/释放机制:在这种情况下,指令将是:

date2=[date1 retain];

但是当它谈到强/弱参考时,这听起来像是我的矛盾:

“默认情况下,引用很强。如果将对象分配给强引用,ARC会假定您希望该对象保持不变并隐式保留它”

这不是以前所说的矛盾吗? 默认情况下,date2很强,因此它应该隐式保留date1,并且不存在错误的访问异常 当然我误解了一些事情,有人能更好地向我解释一下吗?

2 个答案:

答案 0 :(得分:15)

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
}

答案 1 :(得分:3)

关键错误在于将手动保留释放行为与ARC相提并论。事实并非如此。在ARC下,对象分配既是原子的,也是决定对象生命周期的表达式。

举例并删除retain。你最终得到了这个:

NSDate* date1=[[NSDate alloc]init];
NSDate* date2=date1;
NSLog(@"%@",date2);

在ARC下具有完美意义;没有手动释放搞砸了。即它会自动生效。

更好的是,因为编译器在幕后进行流控制分析,所以不需要额外的保留或释放。代码实际上就像是:

NSDate* date1; // date1 initialized to nil -- or not given that it is never read before...
date1 = [[NSDate alloc]init]; // ... this assignment (of a +1 retain count obj)
NSDate* date2=date1; // retain count unchanged
NSLog(@"%@",date2); // retain count unchanged
.... compiler emits equivalent to [date1 release] ...

由于编译器在上次使用release date1之后才会发出date2,因此永远不会有悬空指针。< / p>