我根据字典键创建一个数组:
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 的数组会导致崩溃?非常混乱!