NSString stringWithFormat:用于数字值

发布于 2024-11-28 10:36:50 字数 2074 浏览 3 评论 0原文

过去几天一直在开发这个应用程序,最后才将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-12-05 10:36:50

但是如果我使用“%d”,它会显示为一堆随机数

%d 是 int 的格式说明符,但您传递的是一个对象。如果您只想要数字,请保留 %d 但传入正确的值:

Item *item = [prices objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"£%d", item.price];

或类似的内容,具体取决于您的对象。

But if I use '%d' it comes up as a bunch of random numbers

%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:

Item *item = [prices objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"£%d", item.price];

or something along those lines, depending on your object.

谈场末日恋爱 2024-12-05 10:36:50

查看字符串格式说明符。这将使您的编码更加容易。

Take a look at String Format Specifiers. This will make your coding easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文