【问题标题】:NSKeyedArchiver works in iPhone simulator, fails on deviceNSKeyedArchiver 在 iPhone 模拟器中工作,在设备上失败
【发布时间】:2010-11-01 01:56:40
【问题描述】:

我有一个简单的应用程序,它试图在使用 NSKeyedArchiver/NSKeyedUnarchiver 的调用之间保存一些数据。它在模拟器上运行良好,但是当我将它导出到真正的 iPhone 硬件时,NSKeyedArchiver 无法写入磁盘。

我用的是类方法

[NSKeyedArchiver archiveRootObject:myRootObject toFile:@"data.archive"]

“myRootObject”中的所有对象都实现了 NSCoding 协议。同样,在模拟器上,这返回 YES 并且一切都很好。在设备上,这将返回 NO 并且没有保存任何内容。 (都是2.2.1)

是否有一些写入权限或应用程序需要能够写入手机 HD 的内容?有人看过这个吗?

提前感谢您的帮助。

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    我在这里猜测,但您不会指定指向捆绑包中目录的文件路径吗? 我认为您可以写入 NSBundle 下的 Documents 目录。

    尝试像这样创建文件路径:

    // Documents directory
    NSArray *paths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                        NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    
    // <Application Home>/Documents/foo.arch
    NSString *fooPath =
    [documentsPath stringByAppendingPathComponent:@“foo.arch”];
    

    然后在您使用的方法中将“fooPath”作为文件路径传递给您,看看是否有帮助。

    【讨论】:

      【解决方案2】:

      感谢您的帮助,您让我走上了正确的思路……诀窍是找出应用程序的根目录,然后写入它的子目录。烦人的是模拟器让你在任何地方都可以写。

      以下是解决所有问题的方法:

      NSString *localPath = @"Documents/my.archive";
      NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:localPath];
      [NSKeyedArchiver archiveRootObject:archive toFile:fullPath];
      

      另见similar thread

      【讨论】:

      • 很好,你想通了,我认为 NSSearchPathForDirectoriesInDomains 也会给你你的 Documents 目录。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多