trying to strip out whitespace doesn't work in NSXMLparser foundCharacters method

Discussion in 'iOS Programming' started by ulquiorra, Sep 14, 2009.

  1. ulquiorra macrumors member

    Jun 17, 2009
    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.
    This is my foundCharacters function and I've been trying to use stringByTrimmingCharactersInSet unfortunately with no success.

        - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
        		currentElementValue = [[NSMutableString alloc] initWithString:string];
        		[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 ?
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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?
  3. ulquiorra thread starter macrumors member

    Jun 17, 2009
    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.

Share This Page