Find parent directory of a path
17,686
Solution 1
The NSString
method -stringByDeletingLastPathComponent
does just that.
You can use it like this:
NSLog(@"%@", [@"/tmp/afolder" stringByDeletingLastPathComponent]);
And it will log /tmp
.
Solution 2
Usually file URLs are of type NSURL
. There's now a method you can use to grab the parent directory:
NSURL *parentDirectory = [fileURL URLByDeletingLastPathComponent];
Solution 3
You should use URL for file locations. If you have a path as String I would convert it to URL. For Swift 3 use
let fileURL: URL = URL(fileURLWithPath: "/path/to/something")
let folderURL = fileURL.deletingLastPathComponent()
Author by
indragie
iOS and Mac Developer. I'm working on Flamingo for Mac and previously built Sonora.
Updated on June 04, 2022Comments
-
indragie almost 2 years
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/