类型' FileManager'的值没有会员' urlsForDirectory' - AppDelegate Swift 3错误

时间:2016-08-11 16:02:12

标签: core-data appdelegate swift3 xcode8

由于我最近更新到XCode 8 Beta 5,我一直在尝试在appDelegate核心数据堆栈中解决此错误。

在这些代码行中,我收到以下错误:

// MARK: - Core Data stack

lazy var applicationDocumentsDirectory: URL = {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "fitness.paceapp.Pace" in the application's documents Application Support directory.
    let urls = FileManager.default.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)
    return urls[urls.count-1]
}()

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.urlForResource("Dominos", withExtension: "momd")!
    return NSManagedObjectModel(contentsOf: modelURL)!
}()

enter image description here

关于我可能遗失的任何想法。我很迷茫,那里答案不多。提前致谢。

2 个答案:

答案 0 :(得分:4)

你走了,

lazy var applicationDocumentsDirectory: URL = {
        // The directory the application uses to store the Core Data store file. This code uses a directory named "fitness.paceapp.Pace" in the application's documents Application Support directory.
        let urls = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
        return urls[urls.count-1]
    }()

    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: "Dominos", withExtension: "momd")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()

答案 1 :(得分:1)

是:

let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls.last!

或者

return try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)