I’m parsing an XML document. I have an NSMutableString to hold the value for each node I’m passing through and for specific nodes – I want to update the NewsElement class.
This is my code:
[newsElement setValue:currentElementValue forKey:elementName];
The currentElementValue is the NSMutableString the the elementName is key I want to update. Each field is NSString.
The problem is that instead of copying the string, now my NSString points to the currentElementValue address so all of the fields are always the same…
Presumably copying
currentElementValuewould fix your problem. Have you tried:This will make an immutable copy (i.e. an
NSString) of thecurrentElementValuewhich is anNSMutableString. If you need the copy to be mutable you could use themutableCopymethod instead, for example: