Re: Error: mutating method sent to immutable object
Re: Error: mutating method sent to immutable object
- Subject: Re: Error: mutating method sent to immutable object
- From: Graham Cox <email@hidden>
- Date: Sat, 4 Apr 2009 12:52:03 +1100
On 04/04/2009, at 12:27 PM, Priscila J.V. wrote:
Hello,
I'm trying to do this:
int p;
NSMutableArray* arregloNumeros = [[NSMutableArray alloc] init];
NSString* fileContents = [NSString
stringWithContentsOfFile:@"lineasFinal.txt"];
NSEnumerator* lineFileEnumerator = [[fileContents
componentsSeparatedByString:@"|"] objectEnumerator];
NSString* enumeratedFileLine; // Prepare to process each line of
numbers
NSEnumerator * numberEnumerator;
NSString * numberAsString;
while (enumeratedFileLine = [lineFileEnumerator nextObject])
{
numberEnumerator = [[ enumeratedFileLine
componentsSeparatedByString:@","] objectEnumerator];
while (numberAsString = [numberEnumerator nextObject])
{ // change string to float
float auxFloat = [numberAsString floatValue];
[arregloNumeros addObject:[NSString stringWithFormat:@"%f",
auxFloat]]; } for (p = 0; p < 7; p++)
NSLog(@"Position %d and number %@", p, [arregloNumeros
objectAtIndex:p]);
[arregloNumeros release];
}
But when I run the program in my log window appears the
following message:
-[NSCFArray addObject:]: mutating method sent to immutable object
Does anyone know what is going on? and help me please to fix it.
Thanks in advance.
Priscila
A little formatting goes a long way. Your mail is completely
unreadable. I've reformatted it above.
There are numerous problems here. <arregloNumeros> is allocated once
at the start, but after adding only one object to this list, you try
and print out the first 7 objects. You should get a message that the
index is out of range, which will throw an exception that aborts the
whole thing anyway.
If it made it past that (e.g. if you commented out the logging), you
then release <arregloNumeros> which will deallocate it. Then you have
a stale pointer which points to nothing in particular. When you send -
addObject to the stale pointer, that probably generates the mutation
error message you're seeing depending on what random object the stale
pointer happens to point to.
You need to review the memory management guidelines. http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
However, even once you get the above working it will be sorely
inefficient for what you appear to be trying to do. Also check out
NSScanner.
Tip: carelessness is a waste of your own time - the best programmers
are insanely fastidious almost to the point of it being pathological.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden