mamrezo
mamrezo

Reputation: 2285

How can i update name in plist?

<plist version="1.0">
<dict>
<key>accounts</key>
<array>
    <dict>
        <key>name</key>
        <string>OLDNAME</string>
    </dict>
</array>
</dict>

This is The plist File

Upvotes: 1

Views: 1159

Answers (2)

Nikita Leonov
Nikita Leonov

Reputation: 5704

Maybe It will be too obviously but you can use following ways to modify it:

  1. Go into TextEdit and modify content between
  2. In an Xcode double-click to plist file and edit it with plist editor that provides a more user friendly UI for editing plist.
  3. Write an ObjectiveC code with use of NSKeyedArchiver and NSKeyedUnarchiver classes to load, modify and save dictionary.

Ultimate solution for manipulating dictionaries stored in plists:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@ "plist"];
NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
[plistDictionary setValue:@"newName" forKey:@"testKey"];
[plistDictionary writeToFile:plistPath atomically:YES];

Hope you will adopt this code sample for your own needs.

... Ok, I got it here is a code sample for your particular plist structure:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"yourPlist" ofType:@ "plist"];
NSMutableDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
NSMutableArray *accountsArray = [(NSArray *)[plistDictionary valueForKey:@"accounts"] mutableCopy];
NSMutableDictionary *accountDictionary = [(NSDictionary *)[accountsArray objectAtIndex:0] mutableCopy];

[accountDictionary setValue:@"NewName" forKey:@"name"];
[accountsArray replaceObjectAtIndex:0 withObject:accountDictionary];
[plistDictionary setValue:accountsArray forKey:@"accounts"];

[plistDictionary writeToFile:plistPath atomically:YES];

Upvotes: 2

ccozad
ccozad

Reputation: 1129

Apple has plenty of docs on the topic.

Here is one that should help:

http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFPropertyLists/CFPropertyLists.html

Upvotes: 0

Related Questions