user2453243
user2453243

Reputation: 1

Creating a plist with multiple strings

I have a multiple strings I would like to write to one plist using objective c. Can anyone please tell me exactly how to do this? I appreciate it

Upvotes: 0

Views: 153

Answers (2)

Tron5000
Tron5000

Reputation: 854

Here's one possibility:

// 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);
}

Upvotes: 0

Guillaume Algis
Guillaume Algis

Reputation: 11016

As H2CO3 hinted, you could use NSArray's writeToFile:atomically: method.

For example:

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

Upvotes: 1

Related Questions