NSMutableArray - 应用程序在模拟器中运行但在设备上运行

时间:2010-09-16 23:16:54

标签: iphone ios-simulator nsmutablearray device sigabrt

我的应用程序在模拟器中运行正常,但在设备上运行时,它会以SIGABRT终止。我已将其缩小到以下代码:

- (void)loadImage {
int imageNumber = currentImageNumber;
// check to see if the image has already been downloaded.
// if not, get it and add it to an array
if ([imageData count] < totalNumberOfImages && (imageNumber + 1) > [imageData count]) {
    NSString *urlString = [[mainMenuItem.imageDetails objectAtIndex: imageNumber] objectForKey: @"url"];
    NSURL *url = [NSURL URLWithString: urlString];
    NSData *data = [NSData dataWithContentsOfURL: url];
    UIImage *image = [UIImage imageWithData: data];
    [imageData addObject: image];
}

// set the image from the array
self.imageOnScreen = [imageData objectAtIndex: imageNumber];

}

是[imageData addObject:image];生成崩溃的消息,具有以下控制台输出:

2010-09-17 00:03:03.005 PilotsMate[17426:5e03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x313f4fd3 __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x3302f8a5 objc_exception_throw + 24
    2   CoreFoundation                      0x31377f99 -[__NSArrayM insertObject:atIndex:] + 472
    3   CoreFoundation                      0x31377da7 -[__NSArrayM addObject:] + 34
    4   PilotsMate                          0x00007c5d -[WeatherDetailViewController loadImage] + 300
    5   PilotsMate                          0x00007eb1 -[WeatherDetailViewController displayPicture:] + 232
    6   Foundation                          0x33265c9d -[NSThread main] + 44
    7   Foundation                          0x332ea9e1 __NSThread__main__ + 972
    8   libSystem.B.dylib                   0x3527598d _pthread_start + 248
    9   libSystem.B.dylib                   0x3526b0ec thread_assign_default + 4294967295
)
terminate called after throwing an instance of 'NSException'
[Switching to thread 11523]
[Switching to thread 11523]
Program received signal:  “SIGABRT”.
kill

造成这种情况的原因是什么,因为模拟器中没有问题?我很困惑,很感激你的意见。我所做的就是在viewDidLoad中初始化一个NSMutableArray,然后在数组中添加一个对象。如果我清理目标,会发生什么变化。很奇怪,因为几天前应用程序的这部分运行正常,我根本没有修改过这个实现文件(我还没有更新到最新的iOS 4.1,所以OS或Xcode版本也没有变化)

2 个答案:

答案 0 :(得分:3)

您粘贴的错误消息说明了一切:

[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0

image对象为nil。为什么是nil?在Xcode中运行它时在Xcode中设置一个断点,并找出罪魁祸首上面的哪一行。

答案 1 :(得分:0)

详细说明Shaggy Frog的回答,我认为图像为nil的最可能原因是从URL读取是否有问题。尝试使用dataWithContentsOfURL:options:error:并检查输出的错误,看看发生了什么。