所以我只想知道如何在应用程序第一次运行时将 plist 从捆绑包移动到文档文件夹,因为我需要它。请我不知道该怎么做,所以请帮助我。
问问题
3122 次
3 回答
4
如果您的 plist 名称是“朋友”
只需使用以下代码(它将查找文件是否存在并复制)
NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"friends.plist"];
NSLog(@"plist path %@",destinationPath);
if ([fileManger fileExistsAtPath:destinationPath]){
//NSLog(@"database localtion %@",destinationPath);
return;
}
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"friends.plist:];
[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
这会将 plist 从资源复制到文档目录
于 2012-04-27T16:43:25.373 回答
2
这个函数应该可以解决问题。
- (void)copyPlist {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myplist" ofType:@"plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
}
您不需要在该行中使用 NSError 参数[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
,但它可能对调试有用。
于 2012-04-27T16:42:49.383 回答
0
只是提醒......这来自http://samsoff.es/posts/iphone-plist-tutorial “现在使用JSON比Plists更受欢迎,因为JSON解析器在速度上已经走了很长一段路。它们实际上更快“
于 2012-04-27T16:38:40.867 回答