【发布时间】:2012-04-27 16:31:41
【问题描述】:
所以我只想知道如何在应用程序第一次运行时将 plist 从捆绑包移动到文档文件夹,因为我需要它。请帮帮我。
【问题讨论】:
标签: iphone objective-c ios ipad
所以我只想知道如何在应用程序第一次运行时将 plist 从捆绑包移动到文档文件夹,因为我需要它。请帮帮我。
【问题讨论】:
标签: iphone objective-c ios ipad
如果您的 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 从资源复制到文档目录
【讨论】:
这个函数应该可以解决问题。
- (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];
}
您不需要在[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; 行中使用 NSError 参数,但它可能对调试有用。
【讨论】:
请注意...这来自http://samsoff.es/posts/iphone-plist-tutorial“由于 JSON 解析器在速度上取得了长足的进步,现在使用 JSON 比 Plists 更受欢迎。它们现在实际上比二进制 plists 更快(这很疯狂) .无论如何,请不要在您的 API 中使用 plist。生成它们很慢且不可靠。您应该使用 JSON。句号。"
【讨论】: