문제

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 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]; 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top