在cocoa应用程序的主包中以编程方式创建属性列表文件

时间:2012-08-29 12:00:03

标签: objective-c ios cocoa

  

可能重复:
  iPhone - Writing NSMutableDictionary to file

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

1 个答案:

答案 0 :(得分:0)

我将数组写入plist并使用类似下面的代码读出它。可能有用。

- (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];
}