-1

How to use NSMutable Dictionary in another class for read and write data from plist file in iphone....

Any Ideas?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
donkarai
  • 322
  • 1
  • 3
  • 14

2 Answers2

1

As you can find in in NSDictionary reference, this class has a method to create a nsdictionary from a file initWithContentsOfFile:(NSString *)path . You can do something like:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myDictionary" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

Where myDictionary is your plist name (In case of your plist is inside resource bundle). You can write a plist on disk with the method:

[dict writeToFile:filePath atomically: YES];

Where filePath is your destination path.

Luca Bernardi
  • 4,191
  • 2
  • 31
  • 39
0

See the Plist files example on this tutorial.

// Look in Documents for an existing plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
myPlistPath = [documentsDirectory stringByAppendingPathComponent: 
    [NSString stringWithFormat: @"%@.plist", plistName] ];
[myPlistPath retain];

// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] ) {
    NSString *pathToSettingsInBundle = [[NSBundle mainBundle] 
        pathForResource:plistName ofType:@"plist"];
}


NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath 
    stringByAppendingPathComponent:@"myApp.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];


myKey = (int)[[plist valueForKey:@"myKey"] intValue];
myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue];

[plist setValue:myKey forKey:@"myKey"];
[plist writeToFile:path atomically:YES];
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164