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



默认核心数据代码出于某种原因引发错误。在我试图迁移到新版本的数据模型之后,这种情况才开始发生,因此我确定我做了一些事情来搞砸了。这是相关代码:

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重新生成所需的文件,甚至为什么它首先丢失了。

好。所以我想出了这个...

大约一周前,我将核心数据文件重命名为" Band Manager_2_0"到" BMDATA",在过去一周中,一切都很好。但是,今天,当我迁移数据时,它终于"断开"了连接。

我不知道为什么连接在重命名文件后不会立即断开,这会让我陷入问题。它一定以我今天干扰的方式兑现了位置。

特别令人困惑的是,该代码是为" momd"的扩展而寻找的,但是实际文件的扩展名为.xcdatamodeld ...显然,Xcode 8中的幕后有些东西。

无论如何,都解决了此问题。

最新更新