创建具有多个字符串的plist

时间:2013-06-04 19:48:43

标签: objective-c macos plist

我想要使用目标c写一个plist的多个字符串。任何人都可以告诉我到底该怎么做?我很感激

2 个答案:

答案 0 :(得分:1)

正如H2CO3暗示的那样,您可以使用NSArray的{​​{3}}方法。

例如:

NSArray *arr = @[
    @"my first string",
    @"my second string",
    @"and the last one"
];
[arr writeToFile:@"./out.plist" atomically:NO]; // Or YES depending on your needs

答案 1 :(得分:0)

这是一种可能性:

// Create the path that you want to write your plist to.
NSError *error = nil;
NSURL *documentsURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
if (documentsURL == nil) {
    NSLog(@"Error finding user documents in directory: %@", [error localizedDescription]);
    return nil;
}
NSString *path = [[documentsURL path] stringByAppendingPathComponent:@"YourFile.plist"];

// Populate your strings and save to the plist specified in the above path.
NSString *kRoot = @"kRoot"; 
NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
tempDict[kRoot] = [NSMutableArray array];
[tempDict[kRoot] addObject:@"String 1"];
[tempDict[kRoot] addObject:@"String 2"];
[tempDict[kRoot] addObject:@"String 3"];
// Etc, add all your strings
if (![tempDict writeToFile:path atomically:YES])
{
    NSLog(@"Error writing data to path %@", path);
}