NSString provides a rich set of methods for manipulating strings as file-system paths. You can extract a path’s directory, filename, and extension, expand a tilde expression (such as “~me
”) or create one for the user’s home directory, and clean up paths containing symbolic links, redundant slashes, and references to “.” (current directory) and “..” (parent directory).
- NSString提供了一组丰富的方法,用于将字符串作为文件系统路径进行操作。 您可以提取路径的目录,文件名和扩展名,展开波形符表达式(例如“~me”)或为用户的主目录创建一个表达式,并清理包含符号链接,冗余斜杠和“。”引用的路径。 (当前目录)和“..”(父目录)。
Note: Where possible, you should use instances of NSURL to represent paths—the operating system deals with URLs more efficiently than with string representations of paths.
- 在可能的情况下,您应该使用NSURL的实例来表示路径 - 操作系统比路径的字符串表示更有效地处理URL。
Representing a Path
NSString
represents paths generically with ‘/’ as the path separator and ‘.’ as the extension separator. Methods that accept strings as path arguments convert these generic representations to the proper system-specific form as needed. On systems with an implicit root directory, absolute paths begin with a path separator or with a tilde expression (“~/...
” or “~user/...
”). Where a device must be specified, you can do that yourself—introducing a system dependency—or allow the string object to add a default device.
- NSString一般表示路径,其中'/'作为路径分隔符,'。'作为扩展分隔符。 接受字符串作为路径参数的方法会根据需要将这些通用表示转换为适当的系统特定表单。 在具有隐式根目录的系统上,绝对路径以路径分隔符或波浪表达式(“〜/ ...”或“~user / ...”)开头。 必须指定设备的地方,您可以自己执行此操作 - 引入系统依赖关系 - 或允许字符串对象添加默认设备。
You can create a standardized representation of a path using stringByStandardizingPath. This performs a number of tasks including:
您可以使用stringByStandardizingPath创建路径的标准化表示。 这执行许多任务,包括:
-
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
User Directories
The following examples illustrate how you can use NSString’s path utilities and other Cocoa functions to get the user directories.
- 以下示例说明了如何使用NSString的路径实用程序和其他Cocoa函数来获取用户目录。
// Assuming that users’ home directories are stored in /Users
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];
}
Path Components
NSString
provides a rich set of methods for manipulating strings as file-system paths, for example:
NSString提供了一组丰富的方法,用于将字符串作为文件系统路径进行操作,例如:
Using these and related methods described in NSString Class Reference, you can extract a path’s directory, filename, and extension, as illustrated by the following examples.
- 使用NSString类参考中描述的这些和相关方法,您可以提取路径的目录,文件名和扩展名,如以下示例所示。
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"
File Name Completion
You can find possible expansions of file names using completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:. For example, given a directory ~/Demo
that contains the following files:
- 您可以使用completePathIntoString找到可能的文件名扩展:caseSensitive:matchesIntoArray:filterTypes:。 例如,给定包含以下文件的目录〜/ Demo:
ReadMe.txt readme.html readme.rtf recondite.txt test.txt
you can find all possible completions for the path ~/Demo/r
as follows:
- 你可以找到路径〜/ Demo / r的所有可能的完成,如下所示:
NSString *partialPath = @"~/Demo/r";
NSString *longestCompletion;
NSArray *outputArray;
unsigned allMatches = [partialPath completePathIntoString:&longestCompletion
caseSensitive:NO
matchesIntoArray:&outputArray
filterTypes:nil];
// 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")
网友评论