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

发布于 2024-09-24 03:10:08 字数 2322 浏览 8 评论 0原文

我的应用程序在模拟器中运行良好,但在设备上运行时,它以 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,所以操作系统或 Xcode 版本也没有改变) 。

My app runs fine in the simulator, but when running on the device, it terminates with SIGABRT. I have narrowed it down to the following code:

- (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];

}

It is the [imageData addObject:image]; message that generates the crash, with the following console output:

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

What is causing this, since there are no issues in the simulator? I'm very puzzled and would appreciate your input. All I am doing is initialising an NSMutableArray in viewDidLoad and then adding an object to the array. If I clean the targets, there is no change to what happens. Weird, because this part of the app ran fine a few days ago and I've not modified this implementation file at all (nor have I updated to the newest iOS 4.1 yet, so there is no change to the OS or Xcode version either).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-10-01 03:10:08

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

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

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

The error message you pasted says it all:

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

The image object is nil. Why is it nil? Set a breakpoint in Xcode while running it in the Simulator and figure out which line above it in the culprit.

窝囊感情。 2024-10-01 03:10:08

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

To elaborate on Shaggy Frog's answer, I think the most likely reason for image to be nil is if there is a problem reading from the URL. Try using dataWithContentsOfURL:options:error: and checking the outputted error to see what happened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文