PDA

View Full Version : trying to strip out whitespace doesn't work in NSXMLparser foundCharacters method




ulquiorra
Sep 14, 2009, 11:50 AM
hello all, I'm parsing an xml file and i've been trying to strip out the whitespace characters in my currentElementValue because it's messing up a couple of things.

I can see in my output window that a couple of carriage returns and tabs are present


(gdb) po string
Keep the arms on the side, and lift your leg.
(gdb) po currentElementValue



Keep the arms on the side, and lift your leg.
(gdb)


This is my foundCharacters function and I've been trying to use stringByTrimmingCharactersInSet unfortunately with no success.


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
{

[currentElementValue appendString:string];

currentElementValue = [currentElementValue stringByTrimmingCharactersInSet:
[NSCharacterSet newlineCharacterSet]];
NSString *instructions = @"instructions";
[directions setValue:string forKey:instructions]; //mm
[appDelegate.directions setValue:string forKey:instructions];
//[appDelegate.directions setObject:string forKey:currentElementValue];
// [appDelegate
}
}


I've been getting this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
Which is strange since my currentElementValue is a NSMutableString ..
So what's going wrong ? Does anyone have a clue or idea ?



dejo
Sep 14, 2009, 12:04 PM
Which is strange since my currentElementValue is a NSMutableString ..
Are you sure? We can't be, since we haven't seen the declaration. Just because you alloc it as NSMutableString, doesn't mean it was declared as one.

Also, why do you want to trim the whitespace out inside of foundCharacters:? Why not just do it once, inside of didEndElement:, when you have the entire string to mess with?

ulquiorra
Sep 14, 2009, 03:00 PM
well , in my header I've put



NSMutableString *currentElementValue;


So I'm assuming i've done that correctly.

I found it easier to manipulate the string and currentElementValue in foundCharacters but to be honest it's actually more for testing purposes it makes more sense I guess to do it in didEndElement.