2
我創建基於陣列的字典關鍵的:insertObject:atIndex: - 指數3超出界限爲空數組
factsBuiltArray = [NSMutableArray arrayWithCapacity: 6];
if ([statusDict count] == 10) {
for (NSString *key in [statusDict allKeys]) {
if ([key isEqualToString: @"currenciesAndConversions"]) {
[factsBuiltArray insertObject:key atIndex: 0];
}
else if ([key isEqualToString: @"languageAndTranslations"]) {
[factsBuiltArray insertObject:key atIndex: 1];
}
else if ([key isEqualToString: @"plugSize"]) {
[factsBuiltArray insertObject:key atIndex: 2];
}
else if ([key isEqualToString: @"timezone"]) {
[factsBuiltArray insertObject:key atIndex: 3]; // crashes over here
}
else if ([key isEqualToString: @"population"]) {
[factsBuiltArray insertObject:key atIndex: 4];
}
else if ([key isEqualToString: @"wikipedia"]) {
[factsBuiltArray insertObject:key atIndex: 5];
}
}
}
崩潰日誌是:
*** -[__NSArrayM insertObject:atIndex:]: index 3 beyond bounds for empty array
爲什麼插入對象一個容量爲6的數組使其崩潰?很混亂!
容量NSArrays甚至不是。這只是內存分配的一個提示;該陣列可能會比您傳遞的容量更大(與CFArrays不同)。 – Wevah
好點,NSArray在達到容量時重新分配。感謝您指出@Wevah – NSProgrammer