-1

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
Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52

2 Answers2

0

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")
retainCount
  • 4,478
  • 1
  • 22
  • 14
  • Thank you for your input and time so far, however I tried doing it like this and it doesn't seem to help much, both paths except for the file:// part are completely the same character for character. Container after implementation: https://www.dropbox.com/s/rj8hufkc0yb9lx6/Screenshot%202016-12-17%2001.54.59.png?dl=0 – Antwan van Houdt Dec 17 '16 at 00:55
0

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

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52