应用程序突然抛出NSInvalidArgumentException

时间:2015-07-30 01:21:20

标签: ios objective-c arrays

我的应用会抛出NSInvalidArgumentException。我可以单步执行代码,并且异常似乎在此代码片段中抛出:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Rooms"];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rooms" inManagedObjectContext:managedObjectContext];
    request.resultType = NSDictionaryResultType;
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"roomName"]];

我不擅长阅读下面的堆栈:

2015-07-29 20:30:22.423 HomeSense[55434:2541818] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(
  0   CoreFoundation                      0x00000001120cfc65 __exceptionPreprocess + 165
  1   libobjc.A.dylib                     0x0000000111d68bb7 objc_exception_throw + 45
  2   CoreFoundation                      0x0000000111f94478 -[__NSPlaceholderArray initWithObjects:count:] + 360
  3   CoreFoundation                      0x0000000112007b4f +[NSArray arrayWithObject:] + 47
  4   HomeSense                           0x000000010fe0b78a -[HSPaintLogViewController viewDidLoad] + 666
  5   UIKit                               0x00000001107271d0 -[UIViewController loadViewIfRequired] + 738
  6   UIKit                               0x00000001107273ce -[UIViewController view] + 27
  7   UIKit                               0x0000000110cb432d -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
  8   UIKit                               0x0000000110701d69 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
  9   UIKit                               0x0000000110733248 -[UIViewController _presentViewController:withAnimationController:completion:] + 1761
  10  UIKit                               0x00000001107356c1 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
  11  UIKit                               0x00000001107355e5 -[UIViewController presentViewController:animated:completion:] + 229
  12  UIKit                               0x00000001105f7d62 -[UIApplication sendAction:to:from:forEvent:] + 75
  13  UIKit                               0x00000001105f7d62 -[UIApplication sendAction:to:from:forEvent:] + 75
  14  UIKit                               0x000000011070950a -[UIControl _sendActionsForEvents:withEvent:] + 467
  15  UIKit                               0x00000001107088d9 -[UIControl touchesEnded:withEvent:] + 522
  16  UIKit                               0x0000000110644958 -[UIWindow _sendTouchesForEvent:] + 735
  17  UIKit                               0x0000000110645282 -[UIWindow sendEvent:] + 682
  18  UIKit                               0x000000011060b541 -[UIApplication sendEvent:] + 246
  19  UIKit                               0x0000000110618cdc _UIApplicationHandleEventFromQueueEvent + 18265
  20  UIKit                               0x00000001105f359c _UIApplicationHandleEventQueue + 2066
  21  CoreFoundation                      0x0000000112003431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
  22  CoreFoundation                      0x0000000111ff92fd __CFRunLoopDoSources0 + 269
  23  CoreFoundation                      0x0000000111ff8934 __CFRunLoopRun + 868
  24  CoreFoundation                      0x0000000111ff8366 CFRunLoopRunSpecific + 470
  25  GraphicsServices                    0x0000000114ee7a3e GSEventRunModal + 161
  26  UIKit                               0x00000001105f68c0 UIApplicationMain + 1282
  27  HomeSense                           0x000000010fe06d7f main + 111
  28  libdyld.dylib                       0x0000000113390145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

这个特殊功能一直运行到现在。我完全不知道这里发生了什么,所以任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

您正在尝试将nil值存储到数组中。在商店之前检查nil值为。

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rooms" inManagedObjectContext:managedObjectContext];
    request.resultType = NSDictionaryResultType;
    if([[entity propertiesByName] objectForKey:@"roomName"]!=nil){
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"roomName"]];
    }

答案 1 :(得分:0)

在将对象添加到数组之前检查对象...您可能在尝试在位置0插入nil对象时收到错误。

  

原因:'* - [NSPlaceholderArray initWithObjects:count:]:尝试   从对象[0]'***

插入nil对象

您还可以在代码中使用断点来缩小崩溃发生的位置。