当我从webservice中解析数字时获取(null)

时间:2015-06-01 05:28:52

标签: ios web-services

我必须从webservice显示产品的价格,但值为(null)

 NSArray *arr = [pJson objectForKey:@"Response"];
    NSLog(@"%@",arr);
    for (int i=0;i<arr.count;i++) {
        NSString *thumbnail_data = [[arr objectAtIndex:i]objectForKey:@"img"];
        NSString *ring_data = [[arr objectAtIndex:i]objectForKey:@"name"];
        NSString *id_data = [[arr objectAtIndex:i]objectForKey:@"ID"];
         NSString *cost_data = [[arr objectAtIndex:i]objectForKey:@"productprice"];
        NSLog(@"THUMBNAIL: %@",thumbnail_data);
        NSLog(@"AUTHOR: %@",ring_data);
         NSLog(@"AUTHOR: %@",cost_data);
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      thumbnail_data, img,
                      ring_data,name,id_data,ID,cost_data,cost,
                      nil];
        [myObject addObject:dictionary];
    }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    subCatCell =(ProductCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ProductCell" forIndexPath:indexPath];
    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
    NSMutableString *detail;
    detail = [NSMutableString stringWithFormat:@"%@",
              [tmpDict objectForKey:name]];
   // NSNumber *price = [tmpDict objectForKey:cost];
    NSMutableString *price;
    price = [NSMutableString stringWithFormat:@"%@",
          [tmpDict objectForKey:cost]];
    NSLog(@"%@",price);

   // NSMutableString *catid;
    catid = [NSMutableString stringWithFormat:@"%@",
             [tmpDict objectForKey:ID]];

    NSMutableString *images;
    images = [NSMutableString stringWithFormat:@"%@ ",
              [tmpDict objectForKey:img]];

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:img]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    img= [[UIImage alloc]initWithData:data];

    subCatCell.imageView.image = img;
    subCatCell.lblCost.text = [NSString stringWithFormat:@"%@",price];
    subCatCell.lblName.text = detail;
    return subCatCell;
}

当我记录&#34; cost_data&#34;价格是正确显示但是当我NsLog&#34;价格&#34;在cellForItemIndexPath中我得到&#34;(null)&#34;

1 个答案:

答案 0 :(得分:0)

可能是您没有为NSDictionaryNSMutableArray分配内存。请进行以下更改,以解决您的问题。

第1步:在myObject

中分配viewDidLoad
myObject = [[NSMutableArray alloc] init];

第2步:替换以下代码集。

NSArray *arr = [pJson objectForKey:@"Response"];
    NSLog(@"%@",arr);
    for (int i=0;i<arr.count;i++) {
        NSString *thumbnail_data = [[arr objectAtIndex:i]objectForKey:@"img"];
        NSString *ring_data = [[arr objectAtIndex:i]objectForKey:@"name"];
        NSString *id_data = [[arr objectAtIndex:i]objectForKey:@"ID"];
         NSString *cost_data = [[arr objectAtIndex:i]objectForKey:@"productprice"];
        NSLog(@"THUMBNAIL: %@",thumbnail_data);
        NSLog(@"AUTHOR: %@",ring_data);
         NSLog(@"AUTHOR: %@",cost_data);
      NSDictionary*  dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      thumbnail_data, @"img",
                      ring_data,@"name",id_data,@"ID",cost_data,@"cost",
                      nil];
        [myObject addObject:dictionary];
    }