[ACCEPTED]-Find parent directory of a path-nsfilemanager

Accepted answer
Score: 71

The NSString method -stringByDeletingLastPathComponent does just that.

You can use 1 it like this:

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

And it will log /tmp.

Score: 17

Usually file URLs are of type NSURL. There's 2 now a method you can use to grab the parent 1 directory: NSURL *parentDirectory = [fileURL URLByDeletingLastPathComponent];

Score: 12

You should use URL for file locations. If 2 you have a path as String I would convert 1 it to URL. For Swift 3 use

let fileURL: URL = URL(fileURLWithPath: "/path/to/something")
let folderURL = fileURL.deletingLastPathComponent()

More Related questions