-1

I have a problem when parsing an XML feed. There are approximately 15 child nodes per parent node. Currently I am parsing all nodes using stringbytrimmingchartersinset: then NSCharacterset whitespaceandnewline characterset. Like so:

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
        currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

This works great for all but one of the nodes. For this node I want the text to come through as is. I've tried setting up an if statement to treat this particular node differently than the others so I updated to the following.

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

   if ([testingString isEqualToString:@"AdoptionSummary"]) {
       [currentElementValue appendString:string]; //This keeps breaking
        }
    else {
         currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }

however I'm getting an error.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'

The error only occurs if I use a combination of stringbytrimmingchartersinset and appendString. However if I use either stringbytrimmingchartersinset or appendString on there own it parses without error. currentElementValue is an NSMutableString so why is this error being thrown?

I've tried looking at this NSMutableString appendString generates SIGABRT Error, and this error : 'Attempt to mutate immutable object with appendString:' -- but to no avail.

I would like just 'AdoptionSummary" to append 'as is' while the other 14 are trimmed. What am I doing wrong? Any suggestions?

Thanks!

Community
  • 1
  • 1
kev
  • 2,306
  • 3
  • 26
  • 31

2 Answers2

1

Its because currentElementValue is declared as NSString. Change its type to NSMutableString.

If you have declared this string with NSMutableString then you will have to change object creation also. Change below code:

currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

With this code:

currentElementValue = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

Handle alloc swiftly to avoid memory leak.

Apurv
  • 17,116
  • 8
  • 51
  • 67
  • it is actually declared as an NSMutableString in my .h interface Parser : NSObject { KHMAppDelegate *app; List *theList; NSMutableString *currentElementValue; } property NSString *testingString; -(id)initParser; end – kev Aug 06 '12 at 17:46
  • @Apurvu,`currentElementValue = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; ` did the trick!! Thanks a million! This project is using ARC, do I still need to mannually dealloc? (sorry for the nOOb questions) Thanks Again! – kev Aug 06 '12 at 18:05
  • No, its not required. Also, accept the answer. So it can help to other persons as well. – Apurv Aug 07 '12 at 04:26
0
currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Simply casting an NSString to an NSMutableString doesn't make it one. Make a mutable copy before modifying it.

Nick Veys
  • 23,458
  • 4
  • 47
  • 64