iOS10 + Xcode8 documentDirectory weird behavior

67 views Asked by At

It seems the documentDirectory in Xcode8/Swift3/iOS10, in a framework, on iOS seems unwritable.

API's used / tried:

FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)

( The last one does not seem to be preferred in Swift, which I can understand )

Now, whenever I try to write files to the URL returned in this area I do not seem to be capable of doing so ( both Simulator, and device ). Downloading the container or inspecting it does not show the files either ( I tried several methods of writing ). Also trying to create a directory to write into seems to fail.

The weird thing is that there is no error returned from within API's used or the FileManager itself.

Is there some horrible point I'm missing? Is it a bug I should report? Currently I moved to creating a directory in Library/ instead, as that seems to work and shouldn't be as volatile as Library/Cache/.

Code used to write ( realm.io was used before I decided to do this ):

let URLs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let data = Data.random(32) // Generates a 32 byte long random blob 
try! data.write(to: URLs.last!) // Crashing here with a forced unwrap is fine
2

There are 2 answers

0
Antwan van Houdt On BEST ANSWER

Turns out you need to completely reset your Simulators and restart Xcode. Fun stuff.

1
retainCount On

The path that you are writing to is invalid – you're passing in the directory path instead of the path to the file you want to create. You can craft a path like this:

let path = NSString(string: URLs.last!.path).appendingPathComponent("foo.txt")