Wednesday, May 1, 2013

NSURL lastPathComponent example ios

lastPathComponent
Returns the last path component of a file URL.
- (NSString *)lastPathComponent
Return Value of [NSURL lastPathComponent]
The last path component of the URL.
Example of [NSURL lastPathComponent]
NSString* s = @"http://www.somewhere.org/foo/bar.html/#label";
NSURL* u = [NSURL URLWithString:s];

// Get the last path component from the URL. This doesn't include
// any fragment.
NSString* lastComponent = [u lastPathComponent];

// Find that last component in the string from the end to make sure
// to get the last one
NSRange fragmentRange = [s rangeOfString:lastComponent
                                 options:NSBackwardsSearch];

// Chop the fragment.
NSString* newURLString = [s substringToIndex:fragmentRange.location + fragmentRange.length];

NSLog(@"%@", s);
NSLog(@"%@", newURLString);
Example of [NSURL lastPathComponent]
NSURL *url = [NSURL urlWithString:@"http://www.google.com/a.pdf"];
NSString *filename = [url lastPathComponent];