繁体   English   中英

如何将plist从捆绑包移动到iOS上的文档文件夹

[英]How to move a plist from the bundle to the documents folder on iOS

所以我想知道的是如何在应用程序第一次运行时将plist从bundle移动到documents文件夹,因为我需要它。 请我找不到怎么做,所以帮助我。

如果你的名字是“朋友”

只需使用下面的代码(它将查找文件是否存在并复制)

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];
}

to use the NSError argument in the [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; 你并不使用NSError参数在[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; line,但它可能对调试很有用。

只是一个抬头......这来自http://samsoff.es/posts/iphone-plist-tutorial “自从JSON解析器在速度上走得很远以来,使用JSON比Plists更受欢迎。它们实际上更快现在比二元plists(这是坚果)。无论如何,请,请不要使用plist作为你的API。生成它们是缓慢且不可靠的。你应该使用JSON。期间。“

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM