Wednesday, May 1, 2013

NSURL lastPathComponent example ios

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 = @"";
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

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

NSLog(@"%@", s);
NSLog(@"%@", newURLString);
Example of [NSURL lastPathComponent]
NSURL *url = [NSURL urlWithString:@""];
NSString *filename = [url lastPathComponent];