Blogs icon indicating copy to clipboard operation
Blogs copied to clipboard

深入理解arc中有个疑问想请教

Open github1510 opened this issue 5 years ago • 2 comments

关于strong和weak的理解: NSObject * temp = [[NSObject alloc] init]; __strong NSObject * temp = [[NSObject alloc] init]; 这样有什么区别呢?

arc中没有strong和weak能不能正常运行?

github1510 avatar Oct 10 '20 08:10 github1510

NSObject * temp = [[NSObject alloc] init]; __strong NSObject * temp = [[NSObject alloc] init]; 这样有什么区别呢?

这两行没有任何区别,转换成汇编之后代码是一摸一样的。


arc中没有strong和weak能不能正常运行?

不能,默认变量就是strong的。arc本质上是retain/release/autorelease等函数,strong/weak是给编译器看的,编译器见到strong/weak会选择调用不同的arc函数

LeoMobileDeveloper avatar Oct 12 '20 05:10 LeoMobileDeveloper

我是不是可以这么理解: 在arc中,retain和strong是等同的,他们代表对象引用计数会增加,通过SideTable来实现; weak则不一样,引用计数不会增加,通过弱引用表来实现

github1510 avatar Oct 12 '20 10:10 github1510