【问题标题】:How to move a plist from the bundle to the documents folder on iOS如何将 plist 从捆绑包移动到 iOS 上的文档文件夹
【发布时间】:2012-04-27 16:31:41
【问题描述】:

所以我只想知道如何在应用程序第一次运行时将 plist 从捆绑包移动到文档文件夹,因为我需要它。请帮帮我。

【问题讨论】:

标签: iphone objective-c ios ipad


【解决方案1】:

如果您的 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 从资源复制到文档目录

【讨论】:

    【解决方案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];
    }
    

    不需要[[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; 行中使用 NSError 参数,但它可能对调试有用。

    【讨论】:

      【解决方案3】:

      请注意...这来自http://samsoff.es/posts/iphone-plist-tutorial“由于 JSON 解析器在速度上取得了长足的进步,现在使用 JSON 比 Plists 更受欢迎。它们现在实际上比二进制 plists 更快(这很疯狂) .无论如何,请不要在您的 API 中使用 plist。生成它们很慢且不可靠。您应该使用 JSON。句号。"

      【讨论】:

      • 如果方便比速度更重要怎么办?如果使用 Xcode 内置工具比手动编辑 JSON 文件更有趣、更可靠怎么办?如果 plist 的类型安全是一个重要因素怎么办?
      • 我不相信那个帖子。 BINARY plist 比 JASON 等基于 ASCII 的格式更紧凑,因此它们在网络上的传输速度更快。它们以二进制格式存储值,因此不需要将它们从字符串转换为布尔值/浮点数/整数等。不要使用字符作为分隔符等。我看到了一位我尊敬的高级开发人员的演示文稿很多人比较了 plist、JSON 和 XML。 JSON 的解析速度比 XML 快一些,并且需要更少的内存,但二进制 plist 至少比两者快 10 倍。那是大约一年前,在这个虚假帖子发布之后。
      • 啊,非常有趣的邓肯。如果可能,希望看到该演示文稿的链接!另外,Till,我在引用别人的作品。你的评论似乎有点幼稚。
      猜你喜欢
      • 1970-01-01
      • 2011-05-02
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2019-07-02
      • 2012-04-07
      • 1970-01-01
      相关资源
      最近更新 更多