gpt4 book ai didi

ios - 从返回 SIGABRT 的 Web 服务访问字典对象

转载 作者:行者123 更新时间:2023-11-29 02:51:28 25 4
gpt4 key购买 nike

我有一个表格 View ,我希望在其中的每个单元格中填充来自网络服务的数据。每个单元格都应该显示对象的名称、价格和品牌名称。我可以很好地获得名称,但价格和品牌名称并不那么容易。我检查了网络服务,它确实有价格和品牌名称,所以这肯定是我的代码有问题。到目前为止,它是这样的:

    for (NSDictionary *objItem in resultsArray)
{
NSString *currentItemName = [objectTitlesArray objectAtIndex:indexPath.row];

if ([currentItemName isEqualToString:objItem[@"title"]])
{
cell.nameLabel.text = currentItemName;
cell.priceLabel.text = objItem[@"price"];
cell.brandLabel.text = objItem[@"brands"];

}
}

objectTitlesArray 是我单独制作的数组,其中包含返回的对象的名称,因此我可以轻松获取名称。

这是 resultsArray 中的内容:

<__NSArrayI 0x8cb9900>(
{
asins = (
);
"best_page" = {
currency = USD;
deeplink = "http://www.textbooks.com/iPhone-Book-Covers-iPhone-4S-iPhone-4-and-iPhone-3GS/9780321832764/Scott-Kelby.php";
description = "\"Excellent Marketplace listings for \"\"iPhone Book: Covers iPhone 4S, iPhone 4, and iPhone 3GS\"\" by Scott Kelby starting as low as $1.99!\"";
"image_url" = "http://images.textbooks.com/TextbookInfo/Covers/0321832760.gif";
"in_stock" = 1;
"live_price_url" = "http://api.invisiblehand.co.uk/v1/pages/live_price?url=http%3A%2F%2Fwww.textbooks.com%2FiPhone-Book-Covers-iPhone-4S-iPhone-4-and-iPhone-3GS%2F9780321832764%2FScott-Kelby.php";
"original_url" = "http://www.textbooks.com/iPhone-Book-Covers-iPhone-4S-iPhone-4-and-iPhone-3GS/9780321832764/Scott-Kelby.php";
pnp = "3.99";
price = "24.99";
"price_confidence" = low;
region = us;
"retailer_name" = "Textbooks.com";
title = "iPhone Book: Covers iPhone 4S, iPhone 4, and iPhone 3GS";
};
brands = (
Pearson
);
categories = (
);
eans = (
9780321832764
);
id = 10a9fb4e3868f5289dc0d53af80ae86a;
"image_url" = "http://images.textbooks.com/TextbookInfo/Covers/0321832760.gif";
isbns = (
0321832760
);
models = (
);
mpns = (
0321832760
);
"number_of_pages" = 1;
resource = "/products/10a9fb4e3868f5289dc0d53af80ae86a";
title = "iPhone Book: Covers iPhone 4S, iPhone 4, and iPhone 3GS";
upcs = (
);
},

感谢所有帮助,并提前致谢。

编辑:

将我的代码更改为:

for (NSDictionary *objItem in resultsArray)
{
NSString *currentItemName = [objectTitlesArray objectAtIndex:indexPath.row];

if ([currentItemName isEqualToString:objItem[@"title"]])
{
if (cell.priceLabel.text != (id)[NSNull null] && cell.priceLabel.text.length != 0 && cell.brandLabel.text != (id)[NSNull null] && cell.brandLabel.text.length != 0)
{


cell.nameLabel.text = currentItemName;
NSDictionary *bestPageDictionary = objItem[@"best_page"];
cell.priceLabel.text = bestPageDictionary[@"price"];
NSArray *brandsArray = objItem[@"brands"];
cell.brandLabel.text = [brandsArray firstObject];
}

}
}

现在我得到 NSInvalidArgumentException',原因:'-[__NSCFNumber 长度]:无法识别的选择器发送到实例 0x8c98c00'

最佳答案

您的字典结构与您试图从中获取的数据不匹配。

  • brands 是数组,不是字符串。它恰好包含一个字符串元素,因此您只需要捕获它即可。

    NSArray *brandsArray = objItem[@"brands"];
    cell.brandLabel.text = [brandsArray firstObject];
  • price 不在顶级字典中,而是嵌套在其中的另一个字典中。

    NSDictionary *bestPageDictionary = objItem[@"best_page"];
    cell.priceLabel.text = bestPageDictionary[@"price"];

编辑 看起来价格实际上可能是一个数字,尽管它看起来是一个字符串。试试这个:

NSDictionary *bestPageDictionary = objItem[@"best_page"];
NSNumber *price = bestPageDictionary[@"price"];
cell.priceLabel.text = [NSString stringWithFormat:@"%@", price];

关于ios - 从返回 SIGABRT 的 Web 服务访问字典对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com