0

I am trying to write a Mac app that converts files. I am stuck at the beginning because my app cannot open local files, at least while running in the debugger. I use the NSOpenPanel to create a valid file NSURL:

“file:///Volumes/Seagate_1tib/projects/dataskunk/wasteproduct.xml”

But somewhere in xcode, or the debugger or whatever, this gets mangled into

"/Users/charlweed/Library/Developer/Xcode/DerivedData/dataskunk-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/file:/Volumes/bigdrive/dataskunk/wasteproduct.xml"

Reading the file then fails with a "No such file or directory error".

How do I prevent this mangling during development?

For example, this gives the error, no matter what file is chosen:

let fileResult = openFileDialog("Choose File", message:"Message")
let xmlInFileURLOpt: NSURL? = NSURL.fileURLWithPath(fileResult)
if let xmlInFileURL = xmlInFileURLOpt
  {
    var xmlFileError: NSError?      
    if !xmlInFileURL.checkPromisedItemIsReachableAndReturnError(&xmlFileError){
        println("\(xmlFileError)")
        return
    }
}
Charlweed
  • 1,517
  • 2
  • 14
  • 22
  • This is not because Xcode is mangling your URL, it is because you are not using the URL correctly. Given the `file:` segment in the result path, you seem to be trying to turn it into a filesystem path the incorrect way. Be sure to use `NSURL`'s `path` method if you need a path. – zneak Apr 22 '15 at 17:33
  • See [Get parts of a NSURL in objective-c](http://stackoverflow.com/questions/3692947/get-parts-of-a-nsurl-in-objective-c) – zneak Apr 22 '15 at 17:37
  • I am using the fileUrlWithPath method. In Swift, I take the url string, and use it as an argument to fileUrlWithPath: xmlInFileURL = NSURL.fileURLWithPath(xmlInFilePath) – Charlweed Apr 22 '15 at 18:03
  • `xmlInFilePath` most likely represents an URL instead of a file path. – zneak Apr 22 '15 at 18:11

1 Answers1

0

The answer is that NSURL.fileURLWithPath() does not take a URL-path as an argument, only a filesystem-path. So "file:///Volumes/disk/file.xml" is wrong, "/Volumes/disk/file.xml" is correct.

The mangling is NSURL prefixing the current directory onto what it thinks is a relative filesystem-path String.

Charlweed
  • 1,517
  • 2
  • 14
  • 22