iPhone XML 解析浮点数据
我正在读取 XML 数据并创建对象,但我需要一些对象变量是浮点数。使用 - (void)parser:(NSXMLParser *)parserfoundCharacters:(NSString *)string
它显然变成了一个字符串,我的浮点变量将设置为 0.000000。
在 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName
中,我在设置值时尝试了类似的操作到对象,但它显然是不兼容的类型,因为 setValue 需要一个 (id)key (而且我刚刚意识到 temp 无论如何都设置为 0.000000,所以 floatValue 也不起作用)。
if([elementName isEqualToString:@"longitude"])
{
float temp = [currentElementValue floatValue];
[myObj setValue:temp forKey:elementName];
}
有谁知道如何解决它,或者我只需将其设置为我的对象中的 NSStrings 并随后将其转换为浮点数?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解析的时候直接保存为字符串即可。然后当您需要它进行计算时将其转换为
像上面那样使用
[NSString floatValue]
进行浮动。不过,我认为
float
无法保存NSString
返回的值,因此请尝试使用您的 temp 作为CGFloat
。xml 无法保存字符串以外的任何内容,因此这种方法是可以的。
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 valueNSString
returns so try it with your temp as aCGFloat
instead.There is no way for the xml to hold anything other that strings so this approach is OK.