【问题标题】:NSMutableArray - App runs in simulator but not on deviceNSMutableArray - 应用程序在模拟器中运行,但不在设备上
【发布时间】:2011-04-13 11:50:45
【问题描述】:

我的应用在模拟器中运行良好,但在设备上运行时,它以 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 版本也没有变化) .

【问题讨论】:

    标签: iphone ios-simulator nsmutablearray device sigabrt


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,这是有道理的。我误读了那句话,因为前几天我意外地尝试查询一个 nil 数组时遇到了问题。我收到以下错误: PilotsMate(17524,0x3e9097c8) malloc: purgeable zone does not support guard pages 2010-09-17 00:27:11.768 PilotsMate[17524:5f03] Error Domain=NSCocoaErrorDomain Code=256“操作不能完成。(Cocoa 错误 256。)“UserInfo=0x19fe00 {NSURL=image.weather.com/images/sat/uksat_600x405.jpg}
    • 如果您看到 Jon Rodriguez 的回答,他可能是在正确的轨道上。我认为 256 是网络错误。由于某种原因,模拟器可能无法访问该 URL。您的机器上是否设置了防火墙或代理?您可以在运行 Xcode 的同一台机器上从 Safari 访问该 URL 吗?
    • 这就是为什么我现在知道错误是什么。它在模拟器中运行良好,但 iPhone 在从设备运行时会导致问题。它连接到与 macbook 相同的无线路由器。我可以从 macbook safari 访问图像,但不能从我发现的 iPhone 访问。为什么会这样?
    • 检查无线连接 - 它已经建立了邻居连接。我不觉得自己像个布偶!!!
    • 我们所有的程序员都会时不时地觉得自己像个木偶。这只是轮到你了。 :)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 2012-02-08
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2011-11-10
      相关资源
      最近更新 更多