Sunday, May 12, 2013

NSString pathWithComponents example ios


pathWithComponents:

Returns a string built from the strings in a given array by concatenating them with a path separator between each pair.
+ (NSString *)pathWithComponents:(NSArray *)components
Parameters
components
An array of NSString objects representing a file path. To create an absolute path, use a slash mark (“/”) as the first component. To include a trailing path divider, use an empty string as the last component.
Return Value[ NSString pathWithComponents example ]
A string built from the strings in components by concatenating them (in the order they appear in the array) with a path separator between each pair.
Discussion[ NSString pathWithComponents example ]
This method doesn’t clean up the path created; use stringByStandardizingPath to resolve empty components, references to the parent directory, and so on.
[ NSString pathWithComponents example ]
NSString *fnam = [@"Localizable" stringByAppendingPathExtension:@"strings"];
NSArray *parts = [NSArray arrayWithPathComponents:@"~", @"SP BB", fnam, (void *)nil];
NSString *path = [[NSString pathWithComponents:parts] stringByStandardizingPath];
NSURL *furl = [NSURL fileURLWithPath:path];
[ NSString pathWithComponents example ]
NSString *p = @"/img/david/PlayBasketball.jpg";

NSMutableArray *cmps = [NSMutableArray arrayWitharray:[p pathComponents]];
// cmps will be: ["/", "img", "david", "PlayBasketball.jpg"]

[cmps insertObject:@"HIRes" atIndex:2];
// You want index 2 because "/" is at index 0, and "img" is at index 1.

NSString *newPath = [NSString pathWithComponents:cmps];
// The pathWithComponents method will add the rest of the "/"'s for you
[ NSString pathWithComponents example ]
NSString* filePath = // something

NSArray* pathComponents = [filePath pathComponents];

if ([pathComponents count] > 2) {
   NSArray* lastTwoArray = [pathComponents subarrayWithRange:NSMakeRange([pathComponents count]-2,2)];
   NSString* lastTwoPath = [NSString pathWithComponents:lastTwoArray];
}