2014-12-03 33 views
0

下面代碼生成錯誤如何解決此錯誤「數組元素不能爲零」?

Array element can not be nil

當我在aryResult[0]

NSMutableArray *tempResult = [[NSMutableArray alloc] initWithCapacity:2]; 
switch (habitObj.NoOfTimesPer) 
{ 
    case 1://NO_OF_TIMES_PER_DAY 
    { 
     tempResult = [self getTotalNoOfDaysOrWeeks_createdDate:habitObj.strCreatedDt totalsOf:@"TOTAL_DAYS"]; 
    } 
     break; 
    case 2://NO_OF_TIMES_PER_WEEK 
    { 
     tempResult = [self getTotalNoOfDaysOrWeeks_createdDate:habitObj.strCreatedDt totalsOf:@"TOTAL_WEEKS"]; 
    } 
     break; 
    default: 
     break; 
} 
aryResult[0] = tempResult[0]; 
aryResult[3] = tempResult[1]; 

賦值下面的代碼不會產生錯誤:

[tempResult addObject:[NSNumber numberWithInt:0]]; 
[tempResult addObject:[NSNumber numberWithInt:1]]; 
aryResult[0] = tempResult[0]; 
aryResult[3] = tempResult[1]; 
+0

使該陣列的可變副本不在其他條件下直接分配 – 2014-12-03 09:42:57

回答

0

你的方法

[self getTotalNoOfDaysOrWeeks_createdDate:habitObj.strCreatedDt totalsOf:@"TOTAL_DAYS"];

返回nil,因此tempResultswitch語句內分配後變成nil

調用tempResult[0][tempResult objectAtIndex:0]的簡寫。並返回nil,因爲在Objective-C中,調用nil對象的方法始終返回nil。然後,您嘗試將nil對象添加到NSMutableArray,這是不允許的,所以應用程序崩潰。

+0

是的,它正在工作。謝謝.... – shahnilay86 2014-12-03 10:22:08

+0

如果答案解決了您的問題,您可以將其標記爲已接受:stackoverflow.com/help/someone-answers – 2014-12-03 10:53:13

相關問題