简体   繁体   中英

write to plist file

I am having head ache for a few hours trying to get to write some info to plist file. My plist looks like this:

<plist version="1.0">
<array>
<dict>
    <key>page</key>
    <string>page 1</string>
    <key>description</key>
    <string>description  text 1</string>
</dict>
<dict>
    <key>page</key>
    <string>page 2</string>
    <key>description</key>
    <string>description text 2</string>
</dict>
</array>
</plist>

I just want to write a new entry to plist like page 3 description description text 3

this is the code I use

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"bookmark.plist"]; //
NSMutableDictionary *rootArray = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[rootArray setObject:@"Jimmy1" forKey:@"page"];
    [rootArray setObject:@"Jimmy2" forKey:@"description"];
    [rootArray writeToFile:path atomically:YES];

when I run I do not get any error message but just doest write anything to bookmark.plist, could you give me an idea on how to solve this problem?

thanks

I believe your problem is you have an array of dicts not a root dictionary. So when you init your NSMutableDictionary you're actually getting an array.

I think you need to init an NSMutableArray , add a new dictionary as the object that has the objects you want. then write your array to file.

NSMutableArray *rootArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSDictionary *newPage = [NSDictionary dictionaryWithObjectsAndKeys: @"Page 3", @"page", @"Description text 3", @"description"];

[rootArray addObject:newPage];

[rootArray writeToFile:path atomically:YES];

Haven't checked this in Xcode but I think this is the root of your problem.

Update

Definitely check out Rahul 's answer. He remembered to wrap the write method in an if statement. This is definitely best practice for error handling.

your problem is

NSMutableDictionary *rootArray = [[NSMutableDictionary alloc] initWithContentsOfFile:path];` //it will return you array of dict not dictionary

Try this

 // get your plist file path    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"bookmark.plist"];

// get content of your plist file     
NSMutableArray *rootArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

// create new dictionary with new content
NSDictionary *newPage = [NSDictionary dictionaryWithObjectsAndKeys: @"Page 3", @"page", @"Description text 3", @"description"];

// add new dictionnay to your rootArray
[rootArray addObject:newPage];

if([rootArray writeToFile:path atomically:YES]) // it will return bool value
{
   NSLog(@"Successfully finished writing to file");
}
else
{
    NSLog(@"failed to write");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM