gpt4 book ai didi

objective-c - 添加到数组后保留计数会增加吗?

转载 作者:行者123 更新时间:2023-12-01 17:19:58 25 4
gpt4 key购买 nike

我只是想知道:如果将对象添加到 Objective-C 中的数组或字典中,对象的保留计数会增加吗?我可以在将特定对象添加到数组或字典后立即释放它吗?

最佳答案

是的,它会增加你添加的对象的保留计数,这就是为什么你可以在将对象添加到数组后立即释放它。

NSObject obj1;
obj1=[[NSObject alloc] init];
//obj1's retain count is 1 here.

[array1 addobject:obj1];
//obj1's retain count incremented by 1, so the total retain count is 2.

[obj1 release];
//obj1's retain count decremented by 1, so the total retain count is 1.

array1 将保留对象,直到 array1 本身未被释放。

关于objective-c - 添加到数组后保留计数会增加吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5662085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com