16.2 使用路径:NSPathUtilities.h
NSPathUtilities. h包含了NSString的函数和分类扩展,它允许你操作路径名。应该尽可能地使用这些函数,以便使程序更独立于文件系统结构以及特定文件和目录的位置。代码清单16-5展示了如何使用NSPathUtilities.h提供的几种函数和方法。
代码清单16-5
//Some basic path operations
import<Foundation/NSString.h>
import<Foundation/NSArray.h>
import<Foundation/NSFileManager.h>
import<Foundation/NSAutoreleasePool.h>
import<Foundation/NSPathUtilities.h>
int main(int argc, char*argv[])
{
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];
NSString*fName=@“path.m”;
NSFileManager*fm;
NSStringpath,tempdir,extension,homedir,*fullpath;
NSString*upath=@“~stevekochan/progs/../ch16/./path.m”;
NSArray*components;
fm=[NSFileManager defaultManager];
//Get the temporary working directory
tempdir=NSTemporaryDirectory();
NSLog(@“Temporary Directory is%@”,tempdir);
//Extract the base directory from current directory
path=[fm currentDirectoryPath];
NSLog(@“Base dir is%@”,[path lastPathComponent]);
//Create a full path to the file fName in current directory
fullpath=[path stringByAppendingPathComponent:fName];
NSLog(@“fullpath to%@is%@”,fName, fullpath);
//Get the file name extension
extension=[fullpath pathExtension];
NSLog(@“extension for%@is%@”,fullpath, extension);
//Get users home directory
homedir=NSHomeDirectory();
NSLog(@“Your home directory is%@”,homedir);
//Divide a path into its components
components=[homedir pathComponents];
for(path in components)
NSLog(@,path);
//“Standardize”a path
NSLog(@“@=>%@”,upath,
[upath stringByStandardizingPath]);
[pool drain];
return 0;
}
代码清单16-5输出
Temporary Directory is/var/folders/HT/HTyGLvSNHTuNb6NrMuo7QE+++TI/-Tmp-/
Base dir is examples
fullpath to path.m is/Users/stevekochan/progs/examples/path.m
extension for/Users/stevekochan/progs/examples/path.m is m
Your home directory is/Users/stevekochan
/
Users
stevekochan
~stevekochan/progs/../ch16/./path.m=>/Users/stevekochan/ch16/path.m
函数NSTemporaryDirectory返回系统中可以用来创建临时文件的目录路径名。如果在这个目录中创建临时文件,一定要在完成任务之后将它们删除。另外,还要确保文件名是唯一的,特别是在应用程序的多个实例同时运行时(参见本章末尾的练习5)更应如此。如果多个用户登录到系统,并且运行同一个应用程序,这种情况就很容易发生。
方法lastPathComponent用来从路径中提取最后一个文件名。当你有一个绝对路径名,并且只想从中获取基本文件名时,这个函数很有用。
stringByAppendingPathComponent:方法用于将文件名附加到路径的末尾。如果指定为接收者的路径名不是以斜线结束,那么该方法将在路径名中插入一个斜线,将路径名和附加的文件名分开。结合使用CurrentDirectory方法和stringByAppendingPathComponent:方法,可以在当前目录中创建文件的完整路径名。这种技术展示在代码清单16-5中。
PathExtension方法给出了指定路径名的文件扩展名。在这个例子中,文件path.m的扩展名为m,该方法返回这个扩展名。如果所给的文件没有扩展名,那么方法仅仅返回一个空字符串。
NSHomeDirectory函数返回当前用户的主目录。使用NSHomeDirectoryForUser函数,同时提供用户名作为函数的参数,可以获得任何特定用户的主目录。
PathComponents方法返回一个数组,这个数组包含指定路径的每个组成部分。代码清单16-5按顺序显示了返回数组的每一元素,并且在单独的输出行上显示每个路径组成部分。
最后,和前面讨论的一样,有时路径名包含代字符(~)。FileManager方法将~用作用户主目录的缩写,或将~user用作指定用户的主目录。如果路径名包含代字符,那么使用stringByStandardizingPath方法可以解析它们。这个方法返回一个路径,同时删除这些特殊字符,即将其标准化。如果路径名中出现代字符,则可以使用方法stringByExpandingTildeInPath扩展代字符。
16.2.1 常用的路径处理方法
表16-3总结了许多常用的使用路径方法。其中,components是一个NSArray对象,它包含路径中每一部分的字符串对象;path是一个字符串对象,它指定文件的路径;ext是表示路径扩展名的字符串对象(如,@“mp4”)。
表16-4展示了一些函数,它可用于获取用户、用户的主目录和存储临时文件的目录信息。
你可能还想查看Foundation函数NSSearchPathForDirectoriesInDomains,它可用于查找系统的特殊目录,如Application目录。