• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Error: mutating method sent to immutable object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Error: mutating method sent to immutable object


  • Subject: RE: Error: mutating method sent to immutable object
  • From: Priscila J.V. <email@hidden>
  • Date: Sat, 4 Apr 2009 06:19:22 +0000
  • Importance: Normal

Hello Graham,
thanks a lot.

>
> 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
>
>

_________________________________________________________________
Découvrez toutes les possibilités de communication avec vos proches
http://www.microsoft.com/windows/windowslive/default.aspx_______________________________________________

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

References: 
 >Error: mutating method sent to immutable object (From: Priscila J.V. <email@hidden>)
 >Re: Error: mutating method sent to immutable object (From: Graham Cox <email@hidden>)

  • Prev by Date: Where is "Accessibility Reference for Assistive Applications"?
  • Next by Date: Re: NSString* kMyKey = @"aKey" vs #define MYKEY @"aKey"
  • Previous by thread: Re: Error: mutating method sent to immutable object
  • Next by thread: Error: mutating method sent to immutable object
  • Index(es):
    • Date
    • Thread