Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if i call nsobject init more than once? Does it increase retain count?

I'm pretty new to Objective-C and i have lots of troubles with memory management and still i understand a little. If i have an object, NSArray * myArray for example, and i do this

myArray = [[NSArray alloc] initWithObjects:obj1,obj2,obj3,nil];

then i'm doing something and i want myArray to contain new objects and then i init it again

[myArray initWithObjects:obj4,obj5,obj6, nil];

seems like it does what i need but is it correct grom the point of view of memory management?does it increase retain count? should i release it twice then?

like image 418
Andrey Chernukha Avatar asked Dec 27 '22 11:12

Andrey Chernukha


1 Answers

Don't do that!

In general, if you want to reset the objects or things inside an already existing Objective C object, create and use some kind of Setter method.

For your array, again do not do this! The "initWithObjects" method you cite is a convenience to initialize a immutable (non-changeable) array with the items an array will be populated with over it's entire lifetime.

For what you are trying to do, just use NSMutableArray. The documentation for it is listed below:

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

like image 106
Michael Dautermann Avatar answered Apr 28 '23 17:04

Michael Dautermann