0

I'm reading XML data and creating objects, but I need some of my object variables to be floats. And with the - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string it obviously becomes a string and my float variables will be set to 0.000000.

In the - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName I tried something like this when setting the value to the object, but it's apparently incompatible types as setValue wants a (id)key (and I just realized that temp was set to 0.000000 anyways, so the floatValue doesn't work either).

if([elementName isEqualToString:@"longitude"]) 
{
    float temp = [currentElementValue floatValue];
    [myObj setValue:temp forKey:elementName];
}

Does anyone have any idea how to solve it or do I just have to set it to NSStrings in my object and convert it to floats afterwards?

4

1 回答 1

1

解析时只需将其保存为字符串即可。然后,当您需要它进行计算时,将其转换为浮点数,[NSString floatValue]如上所述。

但是我认为它float不能保存NSString返回的值,所以用你的 temp 来CGFloat代替它。

xml 无法保存除字符串以外的任何内容,因此这种方法是可以的。

于 2010-01-19T13:11:33.003 回答