“FileManager”类型的值没有成员“urlsForDirectory”-AppDelegate Swift 3 错误

Posted

技术标签:

【中文标题】“FileManager”类型的值没有成员“urlsForDirectory”-AppDelegate Swift 3 错误【英文标题】:Value of type 'FileManager' has no member 'urlsForDirectory' - AppDelegate Swift 3 Error 【发布时间】:2016-08-11 16:02:12 【问题描述】:

自从我最近更新到 XCode 8 Beta 5 以来,我一直在尝试解决我的 appDelegate Core Data Stack 中的这个错误。

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

// 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)!
()

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

【问题讨论】:

你看过新的FileManager API reference吗? FileManager and urlsForDirectory Error in Swift 3 Xcode Beta 4的可能重复 【参考方案1】:

给你,

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)!
    ()

【讨论】:

【参考方案2】:

它是:

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)

【讨论】:

以上是关于“FileManager”类型的值没有成员“urlsForDirectory”-AppDelegate Swift 3 错误的主要内容,如果未能解决你的问题,请参考以下文章

“MKMapItem”类型的值在 Swift 3 中没有成员“网站”

类型'URL'没有成员'fileURL' - Swift 3

* 类型的值没有成员 'rx'

“任何”的值类型没有成员 'objectforKey' Swift 3 转换

Swift 3 - 'Any?' 类型的值没有成员“valueForKey”

“String”类型的值没有成员“stringByAppendingPathComponent”[重复]