NSCache崩溃的应用程序

时间:2013-07-07 19:15:22

标签: iphone ios objective-c nscache

我的iOS应用程序项目在控制台说

时使用NSCache时会一直崩溃
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCache setObject:forKey:cost:]: attempt to insert nil value (key: productAllergyCache)'
*** First throw call stack:
(0x1c9a012 0x10d7e7e 0x1c6f7d9 0x1c6f613 0x4211 0xfd1c7 0xfd232 0xfd4da 0x1148e5 0x1149cb 0x114c76 0x114d71 0x11589b 0x115e93 0x115a88 0x2c59 0xcb285 0xcb4ed 0xad55b3 0x1c59376 0x1c58e06 0x1c40a82 0x1c3ff44 0x1c3fe1b 0x1bf47e3 0x1bf4668 0x1bffc 0x20bd 0x1fe5) libc++abi.dylib: terminate called throwing an exception (lldb)

我的代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
     // Do any additional setup after loading the view.
    detailViewmySQL = [[NSCache alloc]init];

    [self.detailViewmySQL setObject:productName forKey:@"productNameCache"];
    [self.detailViewmySQL setObject:productDescription forKey:@"productDescriptionCache" cost:1];
    [self.detailViewmySQL setObject:productImage forKey:@"productImageCache"];
    [self.detailViewmySQL setObject:productAllergy forKey:@"productAllergyCache"];
    [self.detailViewmySQL setObject:productQuantity forKey:@"productQuantityCache"];
    [self.detailViewmySQL setObject:productPrice forKey:@"productPriceCache"];

    self.title = [self.detailViewmySQL objectForKey:@"productNameCache"];

    outProductName.text = [self.detailViewmySQL objectForKey:@"productNameCache"];

    outProductDescription.text = [self.detailViewmySQL objectForKey:@"productDescriptionCache"];
    outProductImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.detailViewmySQL objectForKey:@"productImageCache"]]]];
    outProductAllergy.text = [self.detailViewmySQL objectForKey:@"productAllergyCache"];
    outProductQuantity.text = [self.detailViewmySQL objectForKey:@"productQuantityCache"];
    outProductPrice.text = [self.detailViewmySQL objectForKey:@"productPriceCache"];
}

此处设置数据并传递给上述实施文件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Passing data to view controller
    detailedViewController *productinfo = [self.storyboard instantiateViewControllerWithIdentifier:@"bakeryDetails"];

    //Retrieve current user array
    foodProducts *currentProduct = [bakeryProductArray objectAtIndex:indexPath.row];

    //Pass data to variables of detailedviewcontroller
    productinfo.productName = currentProduct.productName;
    productinfo.productImage = currentProduct.productImgUrl;
    productinfo.productQuantity = currentProduct.productquantity;
    productinfo.productPrice = currentProduct.productPrice;
    productinfo.productAllergy = currentProduct.productAllergy;
    productinfo.productDescription = currentProduct.productDescription;

    [self.navigationController pushViewController:productinfo animated:YES];
}

我已经尝试设置成本和setTotalCostLimit但仍然崩溃,任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否宣布自己的detailViewmySQL为强者?

您可以验证字符串的长度是否为> 0,如果为true,则设置该值。这样可以防止崩溃。