Filemanager and Urlsfordirectory Error in Swift 3 Xcode 8

FileManager and urlsForDirectory Error in Swift 3 Xcode 8

You just need to change it to urls(for:) as follow:

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

Value of type 'FileManager' has no member 'urlsForDirectory' - AppDelegate Swift 3 Error

Here you go,

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

Error: The file...doesn't exist when calling writeToFile on imageData

Instead of [saveLocation absoluteString], use [saveLocation path]. Basically the former gives you "file:///path/filename" while the latter gives you "/path/filename", which is the correct format.

Getting list of files in documents folder

This solution works with Swift 4 (Xcode 9.2) and also with Swift 5 (Xcode 10.2.1+):

let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
// process files
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}

Here's a reusable FileManager extension that also lets you skip or include hidden files in the results:

import Foundation

extension FileManager {
func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL]? {
let documentsURL = urls(for: directory, in: .userDomainMask)[0]
let fileURLs = try? contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: skipsHiddenFiles ? .skipsHiddenFiles : [] )
return fileURLs
}
}

// Usage
print(FileManager.default.urls(for: .documentDirectory) ?? "none")

Swift 3 Core Data issue - All Data Missing

Have you implemented light weight mirgration?

when you initialise your persistent store you can add 'Options', for light weight migration add:

let options = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]

Then when you change the DB on each app version first you must go to the .xcdatamodel and go to:

'editor' > 'Add Model Version' 

In the top menu, then make your changes to this version and make sure it has the green tick on when you open up the model.

Delete files in iOS directory using Swift

I believe your problem is on this line:

let filePaths = try fileManager.contentsOfDirectoryAtPath("\(documentsUrl)")

You're using contentsOfDirectoryAtPath() with something that is an NSURL. You choose either path strings or URLs, not try to mix them both. To pre-empty your possible next question, URLs are preferred. Try using contentsOfDirectoryAtURL() and removeItemAtURL().

Another curious thing you should look at once you resolve the above: why are you using NSTemporaryDirectory() for the file path when you try to delete? You're reading the document directory and should use that.



Related Topics



Leave a reply



Submit