Find parent directory of a path

indragie picture indragie · Aug 21, 2009 · Viewed 16.7k times · Source

Is there any way to find the parent directory of a path using NSFileManager or something?

e.g. Take this:

/path/to/something

And turn it into

/path/to/

Answer

Jon Hess picture Jon Hess · Aug 21, 2009

The NSString method -stringByDeletingLastPathComponent does just that.

You can use it like this:

NSLog(@"%@", [@"/tmp/afolder" stringByDeletingLastPathComponent]);

And it will log /tmp.