plist基本操做

重要概念:某些路径下“只能读,不能写”的缘由html

  iPhone、ipad真机上ios

  Resouces文件夹:是只读的,没法写入。xcode

  document 和temp文件夹:可读,可写函数

1、工程结构atom

  

2、源代码spa

   一、头文件:PlistManage.hcode

@interface PlistManage : NSObject

-(void)resourcePathFileRead;//当前工程资源目录,不一样于真机“沙箱”中的路径

-(NSString *)docPath;//获取document文件夹路径

-(BOOL)isDirNeedCreate:(NSString *)dirPath;//判断目录是否须要新建立

-(BOOL)isFileNeedCreate:(NSString *)filePath;//判断文件是否须要建立
-(void) doAdd; -(void) doRead; -(void) doModify; -(void) doDelete; @end

   二、一些基本函数的实现:htm

//获取document目录路径
-(NSString *)docPath
{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}
//路径是否须要建立
-(BOOL)isDirNeedCreate:(NSString *)dirPath
{
    if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:dirPath] )
    {
        return [[NSFileManager defaultManager] createDirectoryAtPath:dirPath
                                         withIntermediateDirectories:YES
                                                          attributes:nil
                                                               error:NULL];
    }
    
    return NO;
}
//文件是否须要建立
-(BOOL)isFileNeedCreate:(NSString *)filePath{
    if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:filePath] )
    {
        return [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    }
    
    return NO;
}

   三、添加:包括建立不存在的空文件blog

-(void) doAdd{
    
    NSString *docPath=[self docPath];
    NSLog(@"当前docment路径:\n%@",docPath);
    NSString *dataFile=[docPath stringByAppendingPathComponent:@"docData.plist"];
    
    if (YES==[self isFileNeedCreate:dataFile]) {
        NSLog(@"文件原先不存在,现已新建空文件!");
    }else{
        NSLog(@"文件已存在,无需建立!");
    }
    
    NSMutableDictionary *plistDic = [[NSMutableDictionary alloc ] init];
    // 添加2个“单条记录”
    [plistDic setObject:@"shanghai" forKey:@"recordKey001"];
    [plistDic setObject:@"beijing" forKey:@"recordKey002"];
    // 添加2个“字典记录”
    [plistDic setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Jack",@"name",@"22",@"age",nil] forKey:@"dicKey001"];
    [plistDic setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Tom",@"name",@"33",@"age",nil] forKey:@"dicKey002"];
    
    [plistDic writeToFile:dataFile atomically:YES];//彻底覆盖
    NSLog(@"添加内容完成!!");
}

   运行结果:ip

   

   对应路径下生成了新文件:

   

   内容以下

    

   四、读取

-(void) doRead{
    NSString *dataFile=[[self docPath] stringByAppendingPathComponent:@"docData.plist"];
    
    //读取全部内容
    NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:dataFile];
    NSLog(@"完整内容:\n%@",dic);
    
    //读取第一层“字典记录”
    NSDictionary* dicValue=[dic objectForKey:@"dicKey001"];
    NSLog(@"读取第一层“字典记录”:\n%@",dicValue);
    
    //读取第一层“字典记录”中的“子元素”
    NSLog(@"读取第一层“字典记录”中的“子元素”:\nname=%@",[dicValue objectForKey:@"name" ]);
    
    //读取第一层“单条记录”
    NSLog(@"读取第一层“单条记录”:\nrecordKey001=%@",[dic objectForKey:@"recordKey001"]);
}

    运行结果:

    

     五、修改

-(void) doModify{
    NSString *dataFile=[[self docPath] stringByAppendingPathComponent:@"docData.plist"];
    NSMutableDictionary *dic = [[[NSMutableDictionary alloc]initWithContentsOfFile:dataFile]mutableCopy];
    
    //修改“单条记录”
    NSString *city = [dic objectForKey:@"recordKey001"];
    city = @"shanghai-new";
    [dic setObject:city forKey:@"recordKey001"];
    //修改“字典记录”
    NSMutableDictionary *personInfo = [dic objectForKey:@"dicKey001"];
    NSString *name = [dic objectForKey:@"name"];
    name = @"Jack-new";
    [personInfo setValue:name forKey:@"name"];
    [dic setValue:personInfo forKey:@"dicKey001"];
    //写入文件
    [dic writeToFile:dataFile atomically:YES];
    
    NSDictionary* dicResult = [NSDictionary dictionaryWithContentsOfFile:dataFile];
    NSLog(@"修改结果:\n%@",dicResult);
}

    运行结果:

    

   六、删除

-(void) doDelete{
    NSString *dataFile=[[self docPath] stringByAppendingPathComponent:@"docData.plist"];
    NSMutableDictionary *dic = [[[NSMutableDictionary alloc]initWithContentsOfFile:dataFile]mutableCopy];
    //删除“单条记录”
    [dic removeObjectForKey:@"recordKey001"];
    [dic removeObjectForKey:@"dicKey001"];
    //删除“字典记录”
    
    //写入文件
    [dic writeToFile:dataFile atomically:YES];
    
    NSDictionary* dicResult = [NSDictionary dictionaryWithContentsOfFile:dataFile];
    NSLog(@"修改结果:\n%@",dicResult);
}

   运行结果:

   

  各个目录的获取:

  http://www.cnblogs.com/ios-wmm/p/3299695.html

  若是是操做 Resource下的plist文件,可由如下代码完成:只能读,不能写

-(void)resourcePathFileRead{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"resourceData" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"resourceData.plist文件信息以下:\n%@", data);
}

3、ios程序的“沙箱原理”

  xcode6中的模拟器位置:

  /Users/wuxiaofeng/资源库/Developer/

  3个基本目录:

  

  原理:

    a)iTunes在与iPhone同步时,备份全部的Documents和Library文件。
    b)iPhone在重启时,会丢弃全部的tmp文件。

  参考:沙箱,路径获取,文件操做

  http://www.cnblogs.com/dyllove98/archive/2013/07/30/3225955.html

相关文章
相关标签/搜索