我最近更新了一個專案,一兩年沒碰它,現在我遇到了一些問題。
之前一切正常,我什至通過每個錯誤來更新新語法,但我被困在 AppDelegate.Swift 中的一個阻止我運行程式的函式上。這里是這個:
lazy 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.mainBundle.url(forResource: "ProjectName", withExtension: "momd")
return NSManagedObjectModel(contentsOfURL: modelURL ?? <#default value#>)!
}()
在我接受 Xcode 的建議并更新語法后,它看起來像這樣:
lazy 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: "ProjectName", withExtension: "momd")
return NSManagedObjectModel(contentsOf: modelURL ?? <#default value#>)!
}()
我真的很困惑要為默認值設定什么,因為我以前從未編輯或觸摸過 AppDelegate。我需要在這里放什么?
感謝您提供的任何幫助!
uj5u.com熱心網友回復:
由于如果模型丟失,應用程式根本無法運行,因此您可以強制解開 URL
lazy 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: "ProjectName", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
考慮到從 iOS 10、macOS 10.12 開始,有一種更方便的方法來初始化 Core Data 堆疊NSPersistentContainer
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/394794.html
