1

I have a NSMutableArray in my app which I initiate like this:

H-FILE

NSMutableArray *noteBookContent;
@property (nonatomic, retain) NSMutableArray *noteBookContent;

M-FILE

@synthesize noteBookContent;

I then have a method in which I open a txt file and read its contents into a temp NSString and slices this NSString into different bits which are then put into the NSMutableArray. Like so:

    NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
                                                          encoding:NSUTF8StringEncoding
                                                             error:&error];

self.noteBookContent = [[[tempTextOut componentsSeparatedByString: @"\n[DIVIDER]\n"] mutableCopy] autorelease];

My big question is what happens if I repeat the same process a couple of times. Is there a need to release the noteBookContent before I read new data into it? Is there a chance of data being messed up, e.g. if one noteBookContent has 10 items (all called FRUIT) and the next noteBookContent has 5 items (all called SALAD), may I end up with SALAD, SALAD, SALAD, SALAD, SALAD, FRUIT, FRUIT, FRUIT, FRUIT etc.?

Sorry if this is obvious, but I don't really understand what happens in the moment that I read new data into an NSMutableArray which already contains old data.

Thanks for any explanations!

2 Answers 2

8

In this case you’re not actually changing the contents of the array—you’re replacing the object entirely. The old value of noteBookContent—the NSMutableArray instance—gets released by the property assignment (the .noteBookContent = ...), and replaced with a new, separate NSMutableArray that’s created by the -mutableCopy call. A more efficient way to do it would be this:

[self.noteBookContent removeAllObjects];
[self.noteBookContent addObjectsFromArray:[tempTextOut componentsSeparatedByString: @"\n[DIVIDER]\n"]];
5
  • Thanks. Why is this more efficient? Is it actually saver? I like it better as it makes more sense. Sorry if all of this is obvious!
    – n.evermind
    Jul 26, 2011 at 18:47
  • 1
    This way, the existing mutable array doesn’t get deallocated and doesn’t have to clean up its memory—it can reuse the space it already has. Jul 26, 2011 at 18:51
  • @n.evermind - It is maybe more efficient because you do not need to realloc a a new NSMutableArray each time you load a file, I say maybe since emptying the array will in itself have a cost. Which least costly should be measured by Instruments. But if you never actually change the contents of the array after the file is loaded, then using an immutable NSArray is the way to go.
    – PeyloW
    Jul 26, 2011 at 18:52
  • @Noah I actually tested this today and it crashed my app as nothing was found in the noteBookContent anymore. I don't know why this is... your code makes sense, but if I replace it with my line of code, it will crash the app. *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** Call stack at first throw...
    – n.evermind
    Jul 27, 2011 at 10:42
  • This is the code: //[self.noteBookContent removeAllObjects]; //[self.noteBookContent addObjectsFromArray:[tempTextOut componentsSeparatedByString: @"\n[DIV]\n"]]; self.noteBookContent = [[[tempTextOut componentsSeparatedByString: @"\n[DIV]\n"] mutableCopy] autorelease]; If I un-comment the bits you suggested (and get rid of my line), it will crash the app.
    – n.evermind
    Jul 27, 2011 at 10:43
1

You don't need to do anything, because you are using a property. This property automatically handles the memory management of your mutable array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.