iOS Objective-C dealloc方法释放的是什么?

2025-05-11 09:43:46
推荐回答(1个)
回答1:

你自己不应该 dealloc 任何对象,objective-c 里面通过reference counting来管理memory,当一个对象的 reference counting=0时,系统自动会调用dealloc释放此对象,所以在没有auto reference counting 下,你用release, retain就好了,不用dealloc。

auto reference counting(ARC)
然而现在随着ARC的推广,当你不再用一个对象时,编译器(compiler)会自动检查你的代码,看你是否还需要此对象与否,所以现在连retain,release都不用了,有点类似java里面的garbage collection, 但跟java garbage collection又有不同,因为ARC 不管reference cycles,你需要用week,strong来调整reference cycles,不过这里就不多讲了

总之,推荐使用ARC