我有多个字符串,我想使用目标 c 写入一个 plist。谁能告诉我具体怎么做?我很感激
问问题
88 次
2 回答
1
正如 H2CO3 所暗示的,您可以使用NSArray
'writeToFile:atomically:
方法。
例如:
NSArray *arr = @[
@"my first string",
@"my second string",
@"and the last one"
];
[arr writeToFile:@"./out.plist" atomically:NO]; // Or YES depending on your needs
于 2013-06-04T20:28:04.290 回答
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);
}
于 2013-06-04T20:28:38.790 回答