0

我有以下课程:

#import "SharedData.h"
static int selectedCountryIndex;
static NSMutableArray *imageDataObjectsArray;
@implementation SharedData
+(void)insertIntoImageDataObjectsArray:(ImageData *)imageData:(int)index{
    if (!imageDataObjectsArray)
        **imageDataObjectsArray = [[NSMutableArray alloc]init ];**

    [imageDataObjectsArray insertObject:imageData atIndex:index];
}
+(ImageData *)getFromImageDataObjectsArray:(int)index{
    return [imageDataObjectsArray objectAtIndex:index];
}
+(void)setSelectedCountryIndex:(int)selectedCountryIndexArg{
    selectedCountryIndex = selectedCountryIndexArg;
}
+(int)getSelectedCountryIndex{
    return selectedCountryIndex;
}
@end

此类仅用于接受来自一个视图的数据,然后允许另一个视图获取该数据。但是,无论何时调用 insertIntoImageDataObjectsArray 方法,标有星号的行都会导致“EXC_BAD_ACCESS”崩溃。这是对该方法的调用:

[SharedData insertIntoImageDataObjectsArray:imageDataObject :[result doubleValue]-1]; 

有人知道为什么吗?

4

1 回答 1

1

我希望这条线崩溃了:

[imageDataObjectsArray insertObject:imageData atIndex:index];

而且我预计它会崩溃,因为您试图在大于数组的索引处插入。

0 => "value1",
1 => "value2"

[imageDataObjectsArray insertObject:@"value3" atIndex:1];会成功并产生

0 => "value1",
1 => "value3",
2 => "value2"

随后调用[imageDataObjectsArray insertObject:@"value4" atIndex:5];将失败,因为索引 5 > 最大索引 (2)

或者正如评论者指出的那样,负数作为索引也是无效的

于 2012-10-15T16:15:50.643 回答