我不確定這個循環到底是什麼問題,但是我每次運行它時都會收到SIGABRT。根據日誌的問題是當我嘗試將NSNumber添加到循環結尾附近的NSMutable數組。很明顯,我犯了一個基本錯誤,但我不確定問題出在哪裏。在循環中添加到NSMutableArray中的問題
NSArray *toArray = [ourDictionary objectForKey:toString];
NSMutableArray *allValuesMArray = [[NSMutableArray alloc] init];
while (done == NO)
{
if (i == 10)
done = YES;
/*
*The job here is to multiply these three numbers together and store the
*product in the mutable array. It tells me NSObject did not recognize selector
*and then crashes.
*original and multiplyFrom are always the same value, and multiplyTo is updated
*from an array I made above from a p-list.
*I'm hoping I didn't make a ton of rookie mistakes here, but I'm new to dealing with
*NSMutableArray and such.
*/
NSNumber *original = [NSNumber numberWithDouble:convertThis];
NSNumber *multiplyFrom = [NSNumber numberWithDouble:multiply];
NSNumber *multiplyTo = [NSNumber numberWithDouble:[[toArray objectAtIndex:i] doubleValue]];
NSNumber *product = [[NSNumber alloc] init];
product = [NSNumber numberWithDouble:([original doubleValue] *
[multiplyFrom doubleValue] *
[multiplyTo doubleValue])];
[allValuesMArray addObject:product];
//This line ^^^ causes crash
i++;
}
NSArray *returnThisArray = allValuesMArray;
[allValuesMArray autorelease];
return returnThisArray;
你可以在這裏添加日誌歷史嗎? – Sarah 2011-01-21 07:54:03
請顯示更多代碼,特別是`allValuesMArray`的創建。此外,刪除`[產品發佈]`,這將導致稍後崩潰。最後,BOOL類型的正確常量是`YES` /`NO`,而不是`TRUE` /`FALSE` – 2011-01-21 08:10:09
我編輯了代碼以顯示更多的事情,如果它仍然不夠讓我知道你還需要看什麼。循環上面的所有東西都應該可以正常工作,我不確定獲得我想要的東西的最佳方法。這種方法的要點是從toArray讀取數字列表,計算產品,將每個產品存儲到一個新的數組中,然後將返回並寫入到一個p列表中。 – Justin 2011-01-21 08:38:38