开发者

iPhone XML parsing float data

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 whe开发者_StackOverflow社区n 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?


Just save it as a string when parsing. Then when you need it for calculations convert it to a float with [NSString floatValue] as you do above.

However I think that float can't hold the value NSString returns so try it with your temp as a CGFloat instead.

There is no way for the xml to hold anything other that strings so this approach is OK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜