大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > plist基本操作

plist基本操作(2)

关键词:基本操作plist  阅读(1155) 赞(15)

[摘要]本文是对plist基本操作的讲解,对学习IOS苹果软件开发有所帮助,与大家分享。

5、修改

-(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);
}

运行结果:

6、删除

-(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);
}

运行结果:

各个目录的获取:

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

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

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

xcode6中的模拟器位置:

/Users/wuxiaofeng/资源库/Developer/

3个基本目录:

原理:

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

«上一页12下一页»


相关评论