iOS中NSSearchPathForDirectoriesInDomains函数

iOS可操作目录有两种:Documents下与Caches下, 如图:

iOS中NSSearchPathForDirectoriesInDomains函数_第1张图片

iOS中NSSearchPathForDirectoriesInDomains函数_第2张图片

1、创建目录

createDirectoryAtPath: withIntermediateDirectories: attributes: error:
NSFileManager * fileManager = nil;
NSArray *paths = nil; 
NSString *documentsDirectory = nil;
NSString * folerName = @"Photos";
NSString * fileName = @"myphoto.png";
NSString  * filePath = nil;
UIImage *photoimage = nil;
NSData * imageData = nil;

//Documents:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//Caches:
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

documentsDirectory = [[paths objectAtIndex:0]   stringByAppendingPathComponent:folerName];

fileManager = [[NSFileManager alloc]init];
[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:error];

2、创建目录并在目录中存储对象

createFileAtPath:  contents:  attributes
filePath = [documentsDirectory stringByAppendingPathComponent:filename]; 
if((![fileManager fileExistsAtPath: fullPathToFile])) {
    photoimage = [[UIImage alloc] imageNamed:@"photo.png"];
    imageData = UIImagePNGRepresentation(photoimage);
    [fileManager createFileAtPath:filePath contents: imageData attributes:nil];
}
[photoimage release];
[fileManager release];

3、删除目录中指定文件

NSString * filePath = [documentsDirectory stringByAppendingPathComponent: fileName];
[filemanager removeItemAtPath: filePath error:NULL];

4、iPhone目录

iPhone会为每一个应用程序生成一个私有目录,这个目录位于:
/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,

并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次。所以通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过:
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES) 得到。
代码如下:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"path:   %@",path);

打印结果如下:

 path:   /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7/Documents

而通过 NSHomeDirectory()也可以得到程序的目录,代码如下:

    NSString *destPath = NSHomeDirectory();
    NSLog(@"path:   %@",destPath);

打印结果如下:

    path:   /Users/apple/Library/Application Support/iPhone Simulator/4.3/Applications/550AF26D-174B-42E6-881B-B7499FAA32B7

5、返回一个绝对路径用来存放我们需要储存的文件。

(NSString *)dataFilePath {  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsDirectory = [paths objectAtIndex:0];  
    return [documentsDirectory stringByAppendingPathComponent:@"shoppingCar.plist"];  
}  
NSFileManager* fm=[NSFileManager defaultManager];  
if(![fm fileExistsAtPath:[self dataFilePath]]){  
    //下面是对该文件进行制定路径的保存  
    [fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];  
    //取得一个目录下得所有文件名  
    NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];  
    //读取某个文件  
    NSData *data = [fm contentsAtPath:[self dataFilePath]];  
    //或者  
    NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];  
}  

你可能感兴趣的:(iOS之API函数)