2

I'm trying to write a string to disk:

return [fileContent writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:error];

But it always fails with the following error:

Error Domain=NSCocoaErrorDomain Code=4 "The folder “ddd.csv” doesn’t exist." UserInfo=0xc4a08b0 {NSUnderlyingError=0xc49ba60 "The operation couldn’t be completed. No such file or directory", NSFilePath=file://<path to file>, NSUserStringVariant=Folder}

The path to the file seems to be valid (I've obscured it here) and the string (NSMutableString) is definitely not 0 length. This code used to work before Mountain Lion. Can anyone help shed a light on what could be going on?

Edit: The value of path is: file://localhost/Users/cocoaster/Downloads/ddd.csv

ruipacheco
  • 15,025
  • 19
  • 82
  • 138

1 Answers1

5

Since you're using a save panel the result comes back as a URL, so if you need a path, convert the result to a path with the path command: [url path].

Alternately, you can use writeToURL:atomically: if that version is available for whatever class fileContent is.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • 1
    URLs are recommended over paths in modern code, and NSStrings do respond to `writeToURL:atomically:encoding:error:`. – Peter Hosey Sep 02 '12 at 23:08