Wednesday, May 1, 2013

NSURL URLByDeletingLastPathComponent example ios


URLByDeletingLastPathComponent

Returns a new URL made by deleting the last path component from the original URL.
- (NSURL *)URLByDeletingLastPathComponent
Return Value
A new URL with the last path component of the original URL removed.
Discussion of [NSURL URLByDeletingLastPathComponent]
If the original URL represents the root path, the returned URL is identical. Otherwise, if the original URL has only one path component, the new URL is the empty string.
Example of [NSURL URLByDeletingLastPathComponent]
NSString *myURLString = @"http://www.test.com/folder/testfolder";
NSURL *myURL = [NSURL URLWithString:myURLString];
myURL = [myURL URLByDeletingLastPathComponent];
myURLString = [myURL absoluteString];
Example of [NSURL URLByDeletingLastPathComponent]
NSURL *documentDir = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
NSURL *tmpDir = [[documentDir URLByDeletingLastPathComponent] URLByAppendingPathComponent:@"tmp" isDirectory:YES];
NSLog(@"tmpDir: %@", [tmpDir path]);