核心数据:bundle.main.url意外地(突然)返回NIL

时间:2017-04-21 18:28:19

标签: xcode cocoa nsuserdefaults

默认核心数据代码由于某种原因抛出错误。这是在我尝试迁移到新版本的数据模型之后才开始发生的,所以我确定我做了一些事情来解决这个问题。这是相关代码:

enter colazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = Bundle.main.url(forResource: "Band_Manager_2_0", withExtension: "momd")!
    return NSManagedObjectModel(contentsOf: modelURL)!
}()

抛出错误的一行就是这一行:

enter let modelURL = Bundle.main.url(forResource: "Band_Manager_2_0", withExtension: "momd")!code here

这是错误:

    fatal error: unexpectedly found nil while unwrapping an Optional value
2017-04-21 14:17:01.620046-0400 Band Manager 2.0[5826:164435] fatal error: unexpectedly found nil while unwrapping an Optional value
Current stack trace:
0    libswiftCore.dylib                 0x0000000100425160 swift_reportError + 129
1    libswiftCore.dylib                 0x0000000100441b80 _swift_stdlib_reportFatalError + 60
2    libswiftCore.dylib                 0x0000000100231a00 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 342
3    libswiftCore.dylib                 0x00000001003acec0 partial apply for (_fatalErrorMessage(StaticString, StaticString, file : StaticString, line : UInt, flags : UInt32) -> Never).(closure #2) + 109
4    libswiftCore.dylib                 0x0000000100231a00 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 342
5    libswiftCore.dylib                 0x000000010035f4a0 specialized _fatalErrorMessage(StaticString, StaticString, file : StaticString, line : UInt, flags : UInt32) -> Never + 96
6    Band Manager 2.0                   0x000000010005f500 AppDelegate.(managedObjectModel.getter).(closure #1) + 463
7    Band Manager 2.0                   0x000000010005d8d0 AppDelegate.managedObjectModel.getter + 194
8    Band Manager 2.0                   0x000000010005f880 AppDelegate.(persistentStoreCoordinator.getter).(closure #1) + 1115
9    Band Manager 2.0                   0x000000010005dad0 AppDelegate.persistentStoreCoordinator.getter + 211
10   Band Manager 2.0                   0x0000000100061020 AppDelegate.(managedObjectContext.getter).(closure #1) + 35
11   Band Manager 2.0                   0x000000010005dce0 AppDelegate.managedObjectContext.getter + 211
12   Band Manager 2.0                   0x000000010005a0d0 AppDelegate.applicationDidFinishLaunching(Notification) -> () + 51
13   Band Manager 2.0                   0x000000010005d530 @objc AppDelegate.applicationDidFinishLaunching(Notification) -> () + 71
14   CoreFoundation                     0x00007fff85d1c520 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
15   CoreFoundation                     0x00007fff85d1c280 _CFXRegistrationPost + 427
16   CoreFoundation                     0x00007fff85d1c160 ___CFXNotificationPost_block_invoke + 50
17   CoreFoundation                     0x00007fff85cd9f90 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
18   CoreFoundation                     0x00007fff85cd94c0 _CFXNotificationPost + 667
19   Foundation                         0x00007fff8771a955 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
20   AppKit                             0x00007fff839439f6 -[NSApplication _postDidFinishNotification] + 297
21   AppKit                             0x00007fff839437b4 -[NSApplication _sendFinishLaunchingNotification] + 208
22   AppKit                             0x00007fff838069c1 -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] + 552
23   AppKit                             0x00007fff838065a6 -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] + 661
24   Foundation                         0x00007fff87765cfb -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 290
25   Foundation                         0x00007fff87765c31 _NSAppleEventManagerGenericHandler + 102
26   AE                                 0x00007fff86b6ad06 aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned int, unsigned char*) + 544
27   AE                                 0x00007fff86b6ac76 dispatchEventAndSendReply(AEDesc const*, AEDesc*) + 39
28   AE                                 0x00007fff86b6aa71 aeProcessAppleEvent + 312
29   HIToolbox                          0x00007fff85277da8 AEProcessAppleEvent + 55
30   AppKit                             0x00007fff838019c4 _DPSNextEvent + 1833
31   AppKit                             0x00007fff83f7cd72 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
32   AppKit                             0x00007fff837f640d -[NSApplication run] + 926
33   AppKit                             0x00007fff837c0d09 NSApplicationMain + 1237
34   Band Manager 2.0                   0x00000001000613d0 main + 13
35   libdyld.dylib                      0x00007fff9b8ad234 start + 1
(lldb)

我希望有人能够对如何解决这个问题有所了解。我对核心数据比较陌生,它使我的编码完全停顿,当然,应用程序在发布后立即崩溃。

谢谢!

更新:我应该澄清,我理解为什么会发生这种情况的技术原因(没有找到所需的资源),但我不知道如何解决这个问题。它似乎找不到的资源是在过去的某个时刻自动生成的。我似乎无法弄清楚如何让XCODE重新生成所需的文件,甚至是为什么它首先丢失了。

1 个答案:

答案 0 :(得分:1)

确定。所以我想出了这个......

大约一周前,我将核心数据文件重命名为&#34; Band Manager_2_0&#34; to&#34; bmData&#34;,过去一周一切都运转良好。然而,今天,在我迁移数据的同时,它终于打破了#34;连接。

我不知道为什么在重命名文件时连接没有立即中断,这会让我陷入困境。它必须以我今天中断的方式兑现该地点。

特别令人困惑的是,代码被寻找&#34; momd&#34;的扩展名,但实际文件的扩展名为.xcdatamodeld ......显然是XCODE 8中幕后的东西。

无论如何,这个问题已经解决了。