iOS应用程序使用未捕获的NSException终止

时间:2015-05-07 02:02:40

标签: ios core-data crash nsexception

我是iOS编程的新手,我正在完成一个较旧的教程。当我运行我的应用程序时,我一直收到错误。它说,“类型NSException未被捕获的异常。”我很丢失,以下内容来自调试器。

2015-05-06 18:55:11.118 Gilligizer[5728:196574] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001112a8c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010fffcbb7 objc_exception_throw + 45
    2   CoreData                            0x000000010fc55304 -[NSManagedObjectContext executeFetchRequest:error:] + 4740
    3   CoreData                            0x000000010fd6bace __43-[NSFetchedResultsController performFetch:]_block_invoke + 270
    4   CoreData                            0x000000010fd39c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192
    5   libdispatch.dylib                   0x0000000115085614 _dispatch_client_callout + 8
    6   libdispatch.dylib                   0x000000011506b002 _dispatch_barrier_sync_f_invoke + 365
    7   CoreData                            0x000000010fd29ec6 -[NSPersistentStoreCoordinator performBlockAndWait:] + 198
    8   CoreData                            0x000000010fd6b8dc -[NSFetchedResultsController performFetch:] + 572
    9   Gilligizer                          0x000000010e1e3180 -[MasterViewController fetchedResultsController] + 720
    10  Gilligizer                          0x000000010e1e276a -[MasterViewController numberOfSectionsInTableView:] + 58
    11  UIKit                               0x000000010e518b03 -[UITableViewRowData _updateNumSections] + 84
    12  UIKit                               0x000000010e5194f4 -[UITableViewRowData invalidateAllSections] + 69
    13  UIKit                               0x000000010e36477b -[UITableView _updateRowData] + 217
    14  UIKit                               0x000000010e37aa1f -[UITableView numberOfSections] + 27
    15  UIKit                               0x000000010e5810d5 -[UITableViewController viewWillAppear:] + 97
    16  UIKit                               0x000000010e3c0fa1 -[UIViewController _setViewAppearState:isAnimating:] + 487
    17  UIKit                               0x000000010e3ed921 -[UINavigationController _startTransition:fromViewController:toViewController:] + 793
    18  UIKit                               0x000000010e3ee448 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
    19  UIKit                               0x000000010e3eef0e -[UINavigationController __viewWillLayoutSubviews] + 43
    20  UIKit                               0x000000010e539715 -[UILayoutContainerView layoutSubviews] + 202
    21  UIKit                               0x000000010e30ca2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    22  QuartzCore                          0x0000000111b3aec2 -[CALayer layoutSublayers] + 146
    23  QuartzCore                          0x0000000111b2f6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    24  QuartzCore                          0x0000000111b2f546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    25  QuartzCore                          0x0000000111a9b886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    26  QuartzCore                          0x0000000111a9ca3a _ZN2CA11Transaction6commitEv + 462
    27  UIKit                               0x000000010e28aa2d -[UIApplication _reportMainSceneUpdateFinished:] + 44
    28  UIKit                               0x000000010e28b6f1 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2648
    29  UIKit                               0x000000010e28a0d5 -[UIApplication workspaceDidEndTransaction:] + 179
    30  FrontBoardServices                  0x00000001103a45e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
    31  CoreFoundation                      0x00000001111dc41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    32  CoreFoundation                      0x00000001111d2165 __CFRunLoopDoBlocks + 341
    33  CoreFoundation                      0x00000001111d1947 __CFRunLoopRun + 887
    34  CoreFoundation                      0x00000001111d1366 CFRunLoopRunSpecific + 470
    35  UIKit                               0x000000010e289b42 -[UIApplication _run] + 413
    36  UIKit                               0x000000010e28c900 UIApplicationMain + 1282
    37  Gilligizer                          0x000000010e1e15cf main + 111
    38  libdyld.dylib                       0x00000001150b9145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:1)

根据崩溃日志判断,您错过了在Core Data获取请求中提供实体。

答案 1 :(得分:0)

在代码中的某个位置,您正在创建NSFetchRequest。它需要在单个Core Data实体上运行,因此请使用以下方法之一:

+[NSFetchRequest fetchRequestWithEntityName:](最简单的选择)

-[NSFetchRequest initWithEntityName:]

-[NSFetchRequest setEntity:],您需要从某处获得NSEntityDescription。检查其标题以获取您的选项。

例如,如果您的Core Data模型中有一个名为“Account”的实体,您可以执行以下操作:

NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Account"];

请记住,虽然Xcode为您创建的NSManagedObject类的名称默认情况下会与模型匹配,但可以更改(例如,添加前缀,使其成为XYZAccount ),所以请确保查看实际的实体名称,而不是类名。