ios - 应用程序突然抛出 NSInvalidArgumentException

标签 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)

此特定功能直到现在都运行良好。我对这里发生的事情一无所知,因此非常感谢任何帮助。

最佳答案

您正在尝试将 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"]];
    }

关于ios - 应用程序突然抛出 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31713763/

相关文章:

ios - Swift:NSFileManager 子类

ios - 引用 dns_sd.h 中的 DNSSDObjects 和 MonoTouch 中的 DNSServiceResolve

ios - 尝试在用户向 iOS 中的 UITableView 添加行时动态增加 UITableView 的高度

ios - 从 iOS8 上的 Bundle ID 获取应用程序的名称

ios - 是否可以快速将录音机音频文件保存在音乐中?

c++ - Visual Studio 2012 错误 0xC0000005 数组输出

ios - Swift:string.characters.count 为阿拉伯字符串返回错误的数字

iphone - 在 NSArray 中搜索

javascript - 访问名称中具有特定值的特定数组

arrays - 如何将字符串元素数组连接成字符串