Attempting to cut spaces does not work in NSXMLparser foundCharacters method

I am parsing the xml file and I was trying to cross out the whitespace in my currentElementValue because it messed 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, unfortunately, have used the stringByTrimmingCharactersInSet application without 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 get this error *** The application terminated due to the unannounced exception "NSInvalidArgumentException", reason: "Attempting to change an immutable object using appendString:" Which is strange, since my currentElementValue is NSMutableString .. So what's wrong? Does anyone have a clue or idea?

+1
source share
4 answers

Skip this, find your error and solve the memory leak:

First you create an NSMutableString. Fine. (+1 save score)

Then you add another line to your NSMutableString. It's great. (still +1 save score).

Then you crop the newlineCharacterSet, which returns ... autoreleased NSString. Since this object is different from your original object, you have leaked into the original object (since it has a +1 save and you no longer have a pointer to it), and now you have an immutable NSString to load. This means that the next time this method is called, you will try to add a line to NSString that will throw an exception "cannot change immutable object".

Here is a quick way to solve this problem:

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(!currentElementValue) currentElementValue = [[NSMutableString alloc] initWithString:string]; else { [currentElementValue appendString:string]; NSString *trimmedString = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; [currentElementValue setString:trimmedString]; NSString *instructions = @"instructions"; [directions setValue:string forKey:instructions]; //mm [appDelegate.directions setValue:string forKey:instructions]; //[appDelegate.directions setObject:string forKey:currentElementValue]; // [appDelegate } } 

(save the trimmed string to another variable, then use the NSMutableString setString: method to transfer the contents, but without losing the pointer to your NSMutableString)

+7
source
By the way, I suggested that NSXMLParser should provide settings for handling spaces, i.e. keep spaces in the W3.org specification for XML / XSLT - also, I don't like getting a character set for something like cropping. Basically, I wanted to have access to "@text ()", as in the case of the XML / XMSLT conversion. So, I did digging and found this method. Please note that you can add a space line to the accumulated text. At the first attempt, it does not work, but I experiment more and double-check my syntax (ie, Cutting "n patls" :))
 - (void)parser:(NSXMLParser *)parser foundIgnorableWhitespace:(NSString *)whitespaceString 

Parameters: parser Parser object.

whitespaceString A string representing all or part of the ignored whitespace of the current element.

Discussion All white space characters in an element (including carriage returns, tabs, and newlines) may not be provided by calling this method individually. A parser can send several parsers to a delegate: foundIgnorableWhitespace: messages for sending whitespace elements. You must add characters in each call to the current character accumulation.

0
source

I found a clean way to work with node text. It is assumed that you own the XML content / format. Now I am using the CDATA section for my node attribute "text ()". This does not include spaces that I will use to tabulate my XML document.

The code to read is as follows:

 - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSLog(@"%s", __FUNCTION__); NSLog(@"[%@]", CDATABlock); NSString *strData = [[NSString alloc] initWithData:CDATABlock encoding: NSUTF8StringEncoding ]; NSLog(@"convertToString=[%@]", strData); 

}

and the XML snippet is like this (textbook with "famous quotes"):

 <quote><![CDATA[And in the end...in your years.]]></quote> 

and the trace output is as follows:

 2009-11-21 13:17:51.901 Fate[2241:4203] -[XMLReader parser:foundCDATA:] 2009-11-21 13:17:51.901 Fate[2241:4203] [<416e6420 696e2074 68652065 6e642c20 69742773 206e6f74 20746865 20796561 72732069 6e20796f 7572206c 69666520 74686174 20636f75 6e742e20 20497427 73207468 65206c69 66652069 6e20796f 75722079 65617273 2e>] 2009-11-21 13:17:55.774 Fate[2241:4203] convertToString=[And in the end, it not the years in your life that count. It the life in your years.]]> 
0
source
 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { // Removing whitspace and newline characters string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // If no characters are left return if([string length] == 0) return; //Rest of the code for saving the string 
0
source

All Articles