0

可能重复:
iPhone - 将 NSMutableDictionary 写入文件

如何在可可应用程序的 MainBundle 中以编程方式创建属性列表文件。我知道如何读取和写入 plist 文件的内容。

4

1 回答 1

0

I write arrays to a plist and read it out using code similar to the below. May be useful.

- (NSString *)offlineCachePath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cache = [paths objectAtIndex:0];
    NSString *path = [cache stringByAppendingPathComponent:@"NameOfApp"];

    // Check if the path exists, otherwise create it
    if (![fileManager fileExistsAtPath:path]) {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }

    return path;
}

- (NSString *)cachedStuffPath {
    NSString *cachedStuffPath = [[self offlineCachePath] stringByAppendingPathComponent:@"NameOfFile.plist"];
    return cachedStuffPath;
}

- (NSMutableArray *)getCachedStuff {
    return [[NSArray arrayWithContentsOfFile:[self cachedStuffPath]] mutableCopy];
}

- (void)cacheStuff:(NSMutableArray *)stuff {
    [stuff writeToFile:[self cachedStuffPath] atomically:YES];
}
于 2012-08-29T12:06:18.007 回答