2014-04-01 48 views
0

我在我的代碼中有一個重複的錯誤。 構建應用程序時,它會很好地工作,直到我點擊應用程序內購買,然後崩潰。NSArrayM *返回「0個對象」

以下是錯誤日誌:

*終止應用程序由於未捕獲的異常 'NSRangeException',原因: '* - [__ NSArrayM objectAtIndex:]:索引5超出界限爲 空數組'

*第一擲調用堆棧:(0x30fd4f4b 0x3b6a66af 0x30f0b533 0xe03d9 0x11ce01 0x1217d3 0xf696b 0x3379b507 0x319af38b 0x30fa00df 0x30f9fcf7 0x30f9e093 0x30f08c27 0x30f08a0b 0x35bdc283 0x337ac049 0X dee93 0xdede8)的libC++ abi.dylib:與類型NSException(LLDB的未捕獲的異常 )

和相關的代碼終止:

// 
// main.m 
// Syntax 
// 
// 

#import <UIKit/UIKit.h> 

#import "AppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //This line in green with SIGABRT 1 
    } 
} 

當我使用異常的斷點,我得到了以下結果:

- (void) purchase:(NSString*)purchase_id { 
    if (![SKPaymentQueue canMakePayments]) { 
     UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                  message:@"inApp purchase Disabled" 
                  delegate:self 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 

     return; 
    } 
    int ID = 0; 

    if ([purchase_id isEqual: IAP_500_BYTES_PACK]){ 
      ID = 4; 
    } 
    else if ([purchase_id isEqual: IAP_1000_BYTES_PACK]) 
    { 
      ID = 0; 
    } 
    else if ([purchase_id isEqual: IAP_3000_BYTES_PACK]){ 
      ID = 2; 
    } 
    else if ([purchase_id isEqual: IAP_7500_BYTES_PACK]){ 
      ID = 5; 
    } 

    else if ([purchase_id isEqual: IAP_20000_BYTES_PACK]){ 
      ID = 1; 
    } 

    else if ([purchase_id isEqual: IAP_50000_BYTES_PACK]){ 
      ID = 3; 
    } 

    else if ([purchase_id isEqual: IAP_ADD_BACKGROUNDS]){ 
     ID = 6; 
    } 

    else if ([purchase_id isEqual: IAP_FULL_VERSION]){ 
     ID = 7; 
    } 

SKProduct *productSK = [self.productRes objectAtIndex:ID]; // HERE is thread 1, breakpoint 1.1 

if(productSK == nil){ 

     UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
               message:@"Cannot connect to iTunes Connect" 
               delegate:self 
               cancelButtonTitle:@"Dismiss" 
               otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 
    return; 
} 

在局部變量區則表示:

productSK __NSArrayM * @ 「0對象」 0x14d40300

我的問題是我找不到爲什麼 「productSK」 是空的。它被正確設置,我認爲:

@property (strong, nonatomic) NSMutableArray *productRes; 
@synthesize productRes; 

你能幫我嗎?

+0

您在哪裏填寫productRes中的值? – sage444

+0

1)'productSK __NSArrayM * @「0對象」0x14d40300'你最好看self.productRes 2)你用ARC嗎? –

+0

@ sage444 這裏是「self.productRes」: self.productRes = [NSMutableArray new]; (SKProduct * product in response.products){ [self.productRes addObject:product]; NSLog(@「%@」,product.productIdentifier); 不,我不使用ARC – Quaxter

回答

0

您遇到的問題由NSRangeException表示。你試圖從你的數組中獲得第五個元素,但是該數組沒有元素。換句話說,在碰撞時,內容是@[ ]

你應該有一些代碼某處(可能在你的init法),設置self.productRes = ...,在您使用SKProduct *爲了你們的應用內購買加油吧。另外,當你的應用程序崩潰並且調試器將你置於main時,崩潰幾乎從未實際存在。這種情況發生在拋出異常但未捕獲的情況下(所以它會一直延伸到程序的入口點)。發生這種情況時,應設置一個exception breakpoint以查看拋出異常的位置。

+0

嗨wjl! 我只在三個地方有「self.productRes」:第一個是你可以閱讀的那個:** bold **'SKProduct * productSK = [self。productRes objectAtIndex:ID];'順便說一下,我發送了一個異常斷點,你可以閱讀它。 其他兩個地方如下: ** bold **' - (void)sendResponse :(SKProductsResponse *)response {self.productRes = [NSMutableArray new]; (SKProduct * product in response.products){ [self.productRes addObject:product]; NSLog(@「%@」,product.productIdentifier); }' – Quaxter

+0

它看起來像'sendResponse'方法可以正確設置數組。該方法是否被調用? – wjl

+0

不,sendResponse未被調用。 – Quaxter