您的位置:首页 > 移动开发 > IOS开发

IOS plist与沙盒的初步使用

2014-06-23 16:36 148 查看
一般情况下是不能对工程目录下的plist文件进行写操作的。
可以通过沙盒机制,我们直接在程序目录下读写操作。
/*/读取plistNSMutableDictionary *dic=[NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"TKHttpBaseInfo" ofType:@"plist"]];NSLog(@"%@",dic);//修改内容[dic setObject:userName forKey:@"userName"];[dic setObject:password forKey:@"passwordKey"];[dic setObject:apikey forKey:@"apiKey"];//获取应用程序沙盒的Documents目录NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString *path = [paths objectAtIndex:0];//得到完整的文件名NSString *filename=[path stringByAppendingPathComponent:@"TKHttpBaseInfo.plist"];[dic writeToFile:filename atomically:YES];NSLog(@"%@",dic);*/NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)       objectAtIndex:0]stringByAppendingPathComponent:@"TKHttpBaseInfo.plist"];NSFileManager *fm = [[NSFileManager alloc]init];//[fm createFileAtPath:path contents:nil attributes:nil];if (![fm fileExistsAtPath:path]) {if(![fm createFileAtPath:path contents:nil attributes:nil]){NSLog(@"create file error");}else{//NSMutableDictionary *dic = [[[NSMutableDictionary alloc]initWithContentsOfFile:path]mutableCopy];NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:3];[dic setObject:userName forKey:@"userName"];[dic setObject:password forKey:@"passwordKey"];[dic setObject:apikey forKey:@"apiKey"];[dic writeToFile:path atomically:YES];NSLog(@"000---------->>>%@",dic);}}else{NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:path];[dic setObject:userName forKey:@"userName"];[dic setObject:password forKey:@"passwordKey"];[dic setObject:apikey forKey:@"apiKey"];[dic writeToFile:path atomically:YES];NSLog(@"111---------->>>%@",dic);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐