How to get the path/directory component from a NSURL?

Brett picture Brett · Oct 4, 2012 · Viewed 17.1k times · Source

I retrieved a NSURL from a NSSavePanel. I now have this NSURL which gives me the following:

file://localhost/Users/brett/Documents/asdf%20asdf.json

Now, it is easy for me to retrieve just the filename using something like the following:

[[[NSFileManager defaultManager] displayNameAtPath:pathAndFilename] stringByDeletingPathExtension]

This gives me just the localized filename, as expected: asdf%20asdf

So, how do I get the path, like so: file://localhost/Users/brett/Documents/

Answer

Mike Abdullah picture Mike Abdullah · Apr 9, 2013

-[NSURL URLByDeletingLastPathComponent] is the simplest way to achieve this.