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

Ios 从返回SIGABRT的Web服务访问字典对象,ios,objective-c,xcode,web-services,Ios,Objective C,Xcode,Web Services,我有一个表视图,其中的每个单元格都希望从web服务中填充数据。每个单元格都应该显示对象的名称、价格和品牌名称。我可以很好地得到它的名字,但是价格和品牌并不是那么容易得到的。我检查了web服务,它确实有价格和品牌名称,所以我的代码肯定有问题。以下是到目前为止的情况: for (NSDictionary *objItem in resultsArray) { NSString *currentItemName = [objectTitlesArray objectAtIndex:i

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

    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"];

    }
}
ObjectTitleArray是我单独创建的一个数组,其中包含返回的对象的名称,因此我可以轻松获取名称

以下是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 =     (
);
},

现在我收到NSInvalidArgumentException',原因:'-[\uu NSCFNumber length]:发送到实例0x8c98c00的未识别选择器'

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

  • 品牌
    是一个数组,而不是字符串。它恰好包含一个字符串元素,所以您只需要抓住它

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

    NSDictionary *bestPageDictionary = objItem[@"best_page"];
    cell.priceLabel.text = bestPageDictionary[@"price"];
    
Edit看起来价格实际上可能是一个数字,即使它看起来是一个字符串。试试这个:

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

所以看起来我现在得到的是对象,因为现在我得到的是SIGABRT[NSNull length],我添加了一个if语句来检查null对象,现在它与NCSF number length或其他东西崩溃了,看起来其中一个值实际上是
NSNumber
,而不是
NSString
,可能是价格。编辑我的答案,试试看。有些价格显示在我的tableviewcell上。无论如何要修复它吗?这很可能意味着实际的JSON值为空。您需要修复数据本身。
NSDictionary *bestPageDictionary = objItem[@"best_page"];
NSNumber *price = bestPageDictionary[@"price"];
cell.priceLabel.text = [NSString stringWithFormat:@"%@", price];