应用程序在从库中选择媒体时崩溃,但将 nil 元素插入到数组中除外

App crashing on selecting media from library with exception that a nil element is being inserted into array

我有一个旧应用程序,我的公司仍在为其提供遗留支持。

我们的一些客户出现了一个奇怪的错误,但我们这边没有。当用户从 ImagePickerController 中选择一些图像时,它显然会发生。

异常

例外说:

Fatal Exception: NSInvalidArgumentException

-[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

堆栈跟踪

堆栈跟踪是:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x2017b7ea0 __exceptionPreprocess
1  libobjc.A.dylib                0x200989a40 objc_exception_throw
2  CoreFoundation                 0x20172f470 _CFArgv
3  CoreFoundation                 0x2016a85f4 -[__NSPlaceholderArray initWithObjects:count:]
4  CoreFoundation                 0x2017b3600 __createArray
5  CoreFoundation                 0x2016b0584 +[NSArray arrayWithObject:]
6  MyAppName             0x102191378 -[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] (PSImageViewController.m:221)
7  UIKitCore                      0x22e30e30c -[UIImagePickerController _imagePickerDidCompleteWithInfo:]
8  PhotoLibrary                   0x214add528 (Missing)
9  CameraUI                       0x21f38c728 -[CAMImagePickerCameraViewController _handleCapturedImagePickerPhotoWithCropOverlayOutput:]
10 AssetsLibraryServices          0x20f173814 __pl_dispatch_sync_block_invoke
11 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
12 libdispatch.dylib              0x20119f6b0 _dispatch_async_and_wait_invoke
13 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
14 libdispatch.dylib              0x20119e9b4 _dispatch_main_queue_callback_4CF$VARIANT$mp
15 CoreFoundation                 0x201747dd0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16 CoreFoundation                 0x201742c98 __CFRunLoopRun
17 CoreFoundation                 0x2017421cc CFRunLoopRunSpecific
18 GraphicsServices               0x2039b9584 GSEventRunModal
19 UIKitCore                      0x22e985054 UIApplicationMain
20 MyAppName             0x1020d60fc main (main.m:17)
21 libdyld.dylib                  0x201202bb4 start

代码

我的 ImagePickerController 委托代码是:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if([info objectForKey:UIImagePickerControllerEditedImage])
    {
        self.image = [info valueForKey:UIImagePickerControllerEditedImage];
    }
    else
    {
        self.image = [info valueForKey:UIImagePickerControllerOriginalImage];
    }
    [self.imgProperty setImage:self.image];
     [[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];
    [self dismissViewControllerAnimated:YES completion:nil];

}

如您所见,我实际上并没有在这里做任何可能导致该异常的事情。打开 imagePicker 的代码是:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    CLS_LOG(@"Clicked Index %d",buttonIndex);

    if (actionSheet.tag == 1)
    {
        if (buttonIndex != actionSheet.cancelButtonIndex)
        {
            if (buttonIndex == actionSheet.firstOtherButtonIndex)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypeCamera];

            }
            else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; return;
            }

        }
    }
}

跟踪PhotoLibrary 0x214add528 (Missing)中的具体行很好奇。我认为不知何故用户没有 PhotoLibrary 或者不知何故为 Imagepicker 选择了错误的媒体源。或者是

[[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];

导致 self.propertyObj.propertyToAppointment 不知何故为 null 的问题?我检查过,它不能为空。否则此屏幕上会出现更多崩溃。它在我的测试运行中始终有效。

如何修复此崩溃?

错误来自第 221 行。您创建一个数组:

[NSArray arrayWithObject:self.propertyObj.propertyToAppointment]

错误告诉您 self.propertyObj.propertyToAppointmentnil