Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the retain count increase when added to an array?

I just wanted to know: will the retain count of an object be incremented if it is added to an array or dictionary in Objective-C? Can I release a particular object immediately after adding it to an array or dictionary?

like image 846
Hariprasad Avatar asked Feb 27 '26 04:02

Hariprasad


1 Answers

Yes, it will increase the retain count of the object you added, that is why you can release the object immediately after adding it to the array.

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 will keep the object until the array1 itself is not released.

like image 127
Usman.3D Avatar answered Mar 01 '26 20:03

Usman.3D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!