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

Était-ce utile?

La 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]; 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top