代码之家  ›  专栏  ›  技术社区  ›  David van Dugteren

上排鸡/蛋方案

  •  0
  • David van Dugteren  · 技术社区  · 14 年前

    我想读/写到cache.plist

    如果我想读取存储在resources文件夹中的现有premade plist文件,我可以执行以下操作:

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

    但我希望能从iPhone上读到它。

    不能,资源文件夹只能读取。

    所以我需要使用:

    NSDocumentDirectory, NSUserDomain,YES
    

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

    因此,这意味着我不必在启动时乱来乱来复制plist文件的凌乱代码。(除非这是唯一的办法)。

    3 回复  |  直到 14 年前
        1
  •  1
  •   Ben Williams    14 年前

    我知道这不是你真正想要的,但据我所知,将文档放入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
  •  1
  •   Dave DeLong    14 年前

    容易的。先看看它是否在文档目录中。如果不是,请在应用程序的资源文件夹中找到它。( [[NSBundle mainBundle] pathForResource...] ,然后使用 [[NSFileManager defaultManager] copyItemAtPath:...] . 然后使用文件目录中的新副本,不受惩罚。

        3
  •  1
  •   David van Dugteren    14 年前

    最终产品

    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.");
    }