2013-07-23 85 views
0

,如果我跑我的,它說failedTransaction模擬器應用...在應用內購買 - 設備崩潰

,如果我在我的iPhone上運行它,它與此錯誤崩潰:

*終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: '* - [__ NSSetM ADDOBJECT:]:對象不能是零'

這裏:是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

SKProduct * product = (SKProduct *) _products[indexPath.row]; 
cell.textLabel.text = product.localizedTitle; 
[_priceFormatter setLocale:product.priceLocale]; 
cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price]; 


if([[ScoreboardIAPHelper sharedInstance] productPurchased:product.productIdentifier]) { 

    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    cell.accessoryView = nil; 
} else { 
    UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    buyButton.frame = CGRectMake(0, 0, 72, 37); 
    [buyButton setTitle:@"Buy" forState:UIControlStateNormal]; 
    buyButton.tag = indexPath.row; 
    [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.accessoryView = buyButton; 
} 

return cell; 
} 

- (void)buyButtonTapped:(id)sender { 

UIButton *buyButton = (UIButton *)sender; 
SKProduct *product = _products[buyButton.tag]; 

NSLog(@"Buying %@...", product.productIdentifier); 
[[ScoreboardIAPHelper sharedInstance] buyProduct:product]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil]; 



} 

,我在這裏得到我的錯誤:

- (void)provideContentForProductIdentifier:(NSString *)productIdentifier { 

[_purchasedProductIdentifiers addObject:productIdentifier]; 
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier]; 

} 
+1

這行代碼是發生在錯誤? – rmaddy

+1

您發佈的錯誤非常具體。您正嘗試將一個零對象添加到可變數組中。在您發佈的代碼中,您應該檢查以確保'p​​roductIdentifier'不是零 –

+0

該錯誤只發生在我在iPhone上運行應用程序而不在模擬器中。 – Skovie

回答

4
[__NSSetM addObject:]: object cannot be nil 

這意味着你有你正在呼籲addObject:一個可變的集合,而是要傳遞的變量是nil

在您發佈的代碼,你把這個唯一的地方是在該行:

[_purchasedProductIdentifiers addObject:productIdentifier]; 

...所以無論你在呼喚provideContentForProductIdentifier:,你傳遞nil

+0

怎麼只有它在我的手機上不在模擬器上,它只是不能恢復購買的產品。 – Skovie

+0

您還沒有提供足夠的代碼來確定,但如果我不得不猜測,我會說您正在從外部文件加載產品標識符,並且您在代碼中用於文件名的情況與你用於實際文件名的情況。 iOS是區分大小寫的,OS X不是。由於無法找到文件,因此最終會得到'nil',並且您試圖從該'nil'值獲取信息的所有代碼也會返回'nil',最終導致您傳遞給' provideContentForProductIdentifier:'是'nil'。 – Jim

+0

嘿謝謝.....我刪除了我的測試acc,並做了一個新的,現在它的一切工作,一定是在測試acc的錯誤... – Skovie

1

變化_purchasedProductIdentifiers

分配替換_purchasedProductIdentifiers = [NSMutableSet set];

通過_purchasedProductIdentifiers = [[NSMutableSet alloc] init];

+0

這工作對我來說,但我想知道爲什麼它如果你能解釋我的問題......我的問題對於_purchasedProductIdentifiers來說並不是零值,而是與我的測試賬戶「堅持」某些先前的購買嘗試有關。 –