Trying to archive an array of Codable elements.
do {
let data = try PropertyListEncoder().encode(elements)
let success = NSKeyedArchiver.archiveRootObject(data, toFile:self.archiveURL.path)
print(success ? "Successful save" : "Save Failed")
} catch {
print("Save Failed")
}
For some reason path (archiveURL) is constantly wrong:
let archiveURL = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
is returning always URL like this:
file:///Users/userName/Library/Developer/CoreSimulator/Devices/deviceID/data/Documents
but searching through folders I see no /Documents folder. Has something changed recently? It used to work few weeks back (pretty sure). Super annoying and I can't find any workaround/fix for that.
I believe this is a (new-ish) "feature" of the iOS simulators - not all of the ususal directories exist when an app is initially installed.
Fortunately it's easy to create them, e.g. for the applicationSupport directory I do this in my apps:
let fileManager = FileManager.default
let directory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!
if !fileManager.fileExists(atPath: directory) {
try fileManager.createDirectory(atPath: directory, withIntermediateDirectories: true, attributes: nil)
}
This happens in Simulators running iOS 11.x and up.
Before iOS 11, the Simulator created the global (app independent) Documents folder on startup. Since iOS 11.x (don't know if it was a special point release), this does not happen anymore.
Be aware that NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
returns this global documents path only in (unit) tests, not when running the app. If you run the app, you get the application specific Documents folder which is still created when installing the app in the simulator.
This seems to be a bug on Apple side. If you run into this issue (as it seems you have), please file a bug report at https://developer.apple.com/bug-reporting/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With