【问题标题】:PList Chicken/Egg ScenarioPList鸡/蛋场景
【发布时间】:2010-05-18 18:33:17
【问题描述】:

我想读/写到 cache.plist

如果我想读取存储在资源文件夹中的现有预制 plist 文件,我可以去:

path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathWithComponent@"cache.plist"];
NSMutableDictionary *root = ...

但是我想从 iPhone 上阅读它。

不能,Resources 文件夹只能读取。

所以我需要使用:

NSDocumentDirectory, NSUserDomain,YES

那么如何将我的 plist 文件预安装到文档目录位置?

这意味着我不必在启动时处理不整洁的代码来复制 plist 文件。 (除非那是唯一的方法)。

【问题讨论】:

    标签: iphone rewrite plist documents


    【解决方案1】:

    我知道这并不是您真正想要的,但据我所知,将文档放入 Documents 文件夹的唯一方法是将其实际复制到那里......但仅限于第一次启动时。我正在为 sqlite 数据库做类似的事情。代码如下,它可以工作,但请注意它可以做一些清理工作:

    // Creates a writable copy of the bundled default database in the application Documents directory.
    - (void)createEditableCopyOfDatabaseIfNeeded {
        // First, test for existence.
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"WordsDatabase.sqlite3"];
        createdDatabaseOk = [fileManager fileExistsAtPath:writableDBPath];
        if (createdDatabaseOk) return;
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"WordsDatabase.sqlite3"];
        createdDatabaseOk = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    }
    

    只需调用您的 AppDelegate - 真的不会太乱吗?

    【讨论】:

      【解决方案2】:

      简单。首先查看它是否在文档目录中。如果不是,请在应用程序的 Resources 文件夹 ([[NSBundle mainBundle] pathForResource...]) 中找到它,然后使用 [[NSFileManager defaultManager] copyItemAtPath:...] 将其复制到文档目录中。然后使用文档目录中的新副本不受惩罚。

      【讨论】:

      • Primo,我认为这两个答案或多或少都是我需要听到的,有点让我检查一下。感谢 Dave deLong 和 alku83
      【解决方案3】:

      最终产品

      NSString *path = [[NSBundle mainBundle] bundlePath];
      NSString *finalPath = [path stringByAppendingPathComponent:@"Cache.plist"];
      
      
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSError *error;
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *giveCachePath = [documentsDirectory stringByAppendingPathComponent:@"Cache.plist"];
      
      
      BOOL fileExists = [fileManager fileExistsAtPath:giveCachePath];
      
      if (fileExists) {
          NSLog(@"file Exists");
      }
      else {
          NSLog(@"Copying the file over");
          fileExists = [fileManager copyItemAtPath:finalPath toPath:giveCachePath error:&error];
      }
      
      NSLog(@"Confirming Copy:");
      
      BOOL filecopied = [fileManager fileExistsAtPath:giveCachePath];
      
      if (filecopied) {
          NSLog(@"Give Cache Plist File ready.");
      }
      else {
          NSLog(@"Cache plist not working.");
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-08
        • 1970-01-01
        • 2016-03-03
        • 2017-02-28
        • 2010-10-29
        • 2011-02-13
        • 2019-02-21
        • 1970-01-01
        • 2020-05-18
        相关资源
        最近更新 更多