Question

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...

Was it helpful?

Solution

Presumably copying currentElementValue would fix your problem. Have you tried:

[newsElement setValue:[currentElementValue copy] forKey:elementName]; 

This will make an immutable copy (i.e. an NSString) of the currentElementValue which is an NSMutableString. If you need the copy to be mutable you could use the mutableCopy method instead, for example:

[newsElement setValue:[currentElementValue mutableCopy] forKey:elementName]; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top