索引3超出空数组的边界

索引3超出空数组的边界

本文介绍了insertObject:atIndex: - 索引3超出空数组的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据字典键创建一个数组:

I create an array based on a dictionaries key's:

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的数组会使其崩溃?非常令人困惑!

Why does inserting an object to an array that is specified with a capacity of 6 make it crash? Very confusing!

推荐答案

容量只是容器类可容纳的对象数。在索引处插入要求索引是基于容器中包含的对象总数(而不是可以包含的对象总数)的新对象的有效放置。

The capacity is merely how many objects a container class can hold. Inserting at an index requires that index to be a valid placement for the new object based on the total number of objects contained in the container (not the total number of objects that CAN be contained).

如果你的数组的值是依赖于索引的(似乎可能是不同的体系结构或数据结构会更好),那么你可以通过使用NSNull预填充数组来确保填充每个索引。这将要求您在稍后从数组中读取时检查NSNull虽然这可能是额外的工作,因此为什么这可能不是最好的方法。在任何情况下,您都可以将代码更改为以下内容以修复崩溃。

If your array's values are index dependent (which is seems like perhaps a different architecture or data structure would be better) then you can ensure that every index is filled by prepopulating the array with NSNulls. This would require you to check for NSNulls when reading from the array later on though which would likely be extra work, hence why this is probably not the best approach. In any case, you can change your code to the following to fix your crash.

factsBuiltArray = [NSMutableArray arrayWithCapacity: 6];

for (NSUInter i = 0; i < 6; i++) {
    [factsBuiltArray addObject:[NSNull null]];
}

if ([statusDict count] == 10) {

    for (NSString *key in [statusDict allKeys]) {

        if ([key isEqualToString: @"currenciesAndConversions"]) {

            [factsBuiltArray replaceObjectAtIndex:0 withObject:key];
        }
        else if ([key isEqualToString: @"languageAndTranslations"]) {

            [factsBuiltArray replaceObjectAtIndex:1 withObject:key];
        }
        else if ([key isEqualToString: @"plugSize"]) {

            [factsBuiltArray replaceObjectAtIndex:2 withObject:key];
        }
        else  if ([key isEqualToString: @"timezone"]) {

        [factsBuiltArray replaceObjectAtIndex:3 withObject:key];

        }
        else if ([key isEqualToString: @"population"]) {

            [factsBuiltArray replaceObjectAtIndex:4 withObject:key];
        }
        else if ([key isEqualToString: @"wikipedia"]) {

            [factsBuiltArray replaceObjectAtIndex:5 withObject:key];
        }
    }
}

这篇关于insertObject:atIndex: - 索引3超出空数组的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:33