NSString stringWithFormat:用于数字值
过去几天一直在开发这个应用程序,最后才将 XML 数据解析为 UITableView。但主要问题是表上显示的实际解析数据并不是应有的显示方式。
例如,我有一个元素(在 XML 文件上),其值为“134.9”(不带引号),如果我使用 '[NSString stringWithFormat:@"£%@", [prices objectAtIndex:indexPath.row ]];'它在表中显示为 '{ Lowest = "134.9\n\t"; }'。
但如果我使用“%d”,它会显示为一堆随机数,甚至不匹配 134.9?
所以这里是代码:
cell.textLabel.text = [NSString stringWithFormat:@"£%d", [prices objectAtIndex:indexPath.row]];
XML 的设置方式:
<PetrolPrices>
<Fuel type="Unleaded">
<Highest units="p">145.9</Highest>
<Average units="p">135.2</Average>
<Lowest units="p">131.9</Lowest>
</Fuel>
<Fuel type="Diesel">
<Highest units="p">149.9</Highest>
<Average units="p">139.3</Average>
<Lowest units="p">135.9</Lowest>
</Fuel>
<Fuel type="LRP">
<Highest units="p">136.9</Highest>
<Average units="p">136.9</Average>
<Lowest units="p">136.9</Lowest>
</Fuel>
<Fuel type="Super Unleaded">
<Highest units="p">152.9</Highest>
<Average units="p">142.3</Average>
<Lowest units="p">135.9</Lowest>
</Fuel>
<Fuel type="LPG">
<Highest units="p">76.9</Highest>
<Average units="p">76.9</Average>
<Lowest units="p">76.9</Lowest>
</Fuel>
<Link>
</Link>
</PetrolPrices>
最后,我使用解析器调用它的部分:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"Fuel"])
{
[item setObject:fuel_price forKey:@"Lowest"];
[item setObject:fuel_type forKey:@"type"];
[prices addObject:[item copy]];
NSLog(@"adding fuel prices: %@ - %@", fuel_price, fuel_type);
}
}
另外,如果有人可以告诉我如何获取属性,就像在 XML
,我似乎无法检索到它。
提前致谢 :)
Been working on this app for the past few days, and only finally just got the XML data parsed into a UITableView. But the main issue is the actual data parsed that's displayed on the table isn't how it should be displayed.
For example I've got one element (on XML file) that has a value of "134.9" (without the quotes), and if I use '[NSString stringWithFormat:@"£%@", [prices objectAtIndex:indexPath.row]];' it displays in the table as '{ Lowest = "134.9\n\t"; }'.
But if I use '%d' it comes up as a bunch of random numbers, that doesn't even match 134.9?
So here's the code:
cell.textLabel.text = [NSString stringWithFormat:@"£%d", [prices objectAtIndex:indexPath.row]];
This how the XML is setup:
<PetrolPrices>
<Fuel type="Unleaded">
<Highest units="p">145.9</Highest>
<Average units="p">135.2</Average>
<Lowest units="p">131.9</Lowest>
</Fuel>
<Fuel type="Diesel">
<Highest units="p">149.9</Highest>
<Average units="p">139.3</Average>
<Lowest units="p">135.9</Lowest>
</Fuel>
<Fuel type="LRP">
<Highest units="p">136.9</Highest>
<Average units="p">136.9</Average>
<Lowest units="p">136.9</Lowest>
</Fuel>
<Fuel type="Super Unleaded">
<Highest units="p">152.9</Highest>
<Average units="p">142.3</Average>
<Lowest units="p">135.9</Lowest>
</Fuel>
<Fuel type="LPG">
<Highest units="p">76.9</Highest>
<Average units="p">76.9</Average>
<Lowest units="p">76.9</Lowest>
</Fuel>
<Link>
</Link>
</PetrolPrices>
Lastly, the bit where I call it using the parser:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"Fuel"])
{
[item setObject:fuel_price forKey:@"Lowest"];
[item setObject:fuel_type forKey:@"type"];
[prices addObject:[item copy]];
NSLog(@"adding fuel prices: %@ - %@", fuel_price, fuel_type);
}
}
Also if someone could tell me how I could get the attributes as well like for in the XML <Fuel type="Unleaded">
, I can't seem to be able to retrieve this.
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%d 是 int 的格式说明符,但您传递的是一个对象。如果您只想要数字,请保留 %d 但传入正确的值:
或类似的内容,具体取决于您的对象。
%d is the format specifier for an int, but you're passing in an object. If you just want the number, keep the %d but pass in the correct value:
or something along those lines, depending on your object.
查看字符串格式说明符。这将使您的编码更加容易。
Take a look at String Format Specifiers. This will make your coding easier.