ou can create a standardized representation of a path using stringByStandardizingPath
. This performs a number of tasks including:
-
Expansion of an initial tilde expression;
-
Reduction of empty components and references to the current directory (“//” and “/./”) to single path separators;
-
In absolute paths, resolution of references to the parent directory (“..”) to the real parent directory;
for example:
NSString *path = @"/usr/bin/./grep"; |
NSString *standardizedPath = [path stringByStandardizingPath]; |
// standardizedPath: /usr/bin/grep |
|
path = @"~me"; |
standardizedPath = [path stringByStandardizingPath]; |
// standardizedPath (assuming conventional naming scheme): /Users/Me |
|
path = @"/usr/include/objc/.."; |
standardizedPath = [path stringByStandardizingPath]; |
// standardizedPath: /usr/include |
|
path = @"/private/usr/include"; |
standardizedPath = [path stringByStandardizingPath]; |
// standardizedPath: /usr/include |
NSString *meHome = [@"~me" stringByExpandingTildeInPath]; |
// meHome = @"/Users/me" |
|
NSString *mePublic = [@"~me/Public" stringByExpandingTildeInPath]; |
// mePublic = @"/Users/me/Public" |
You can find the home directory for the current user and for a given user with NSHomeDirectory
and NSHomeDirectoryForUser
respectively:
NSString *currentUserHomeDirectory = NSHomeDirectory(); |
NSString *meHomeDirectory = NSHomeDirectoryForUser(@"me"); |
Note that you should typically use the function NSSearchPathForDirectoriesInDomains
to locate standard directories for the current user. For example, instead of:
NSString *documentsDirectory = |
[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; |
you should use:
NSString *documentsDirectory; |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
if ([paths count] > 0) { |
documentsDirectory = [paths objectAtIndex:0]; |
} |
NSString *documentPath = @"~me/Public/Demo/readme.txt"; |
|
NSString *documentDirectory = [documentPath stringByDeletingLastPathComponent]; |
// documentDirectory = @"~me/Public/Demo" |
|
NSString *documentFilename = [documentPath lastPathComponent]; |
// documentFilename = @"readme.txt" |
|
NSString *documentExtension = [documentPath pathExtension]; |
// documentExtension = @"txt"
|
NSString *partialPath = @"~/Demo/r"; |
NSString *longestCompletion; |
NSArray *outputArray; |
|
unsigned allMatches = [partialPath completePathIntoString:&longestCompletion |
caseSensitive:NO |
matchesIntoArray:&outputArray |
filterTypes:NULL]; |
|
// allMatches = 3 |
// longestCompletion = @"~/Demo/re" |
// outputArray = (@"~/Demo/readme.html", "~/Demo/readme.rtf", "~/Demo/recondite.txt") |
You can find possible completions for the path ~/Demo/r
that have an extension “.txt” or “.rtf” as follows:
NSArray *filterTypes = @[@"txt", @"rtf"]; |
|
unsigned textMatches = [partialPath completePathIntoString:&outputName |
caseSensitive:NO |
matchesIntoArray:&outputArray |
filterTypes:filterTypes]; |
// allMatches = 2 |
// longestCompletion = @"~/Demo/re" |
// outputArray = (@"~/Demo/readme.rtf", @"~/Demo/recondite.txt") |