how to write NSMutableDictionary into Plist
Asked Answered
Q

5

3

can any one help me with this how to write a NSMutableDictionary into a plist....

thanks in advance..

Quianaquibble answered 21/10, 2010 at 6:7 Comment(0)
C
6

Make sure the pList file you are writing to is located in a legal place to edit it, for example Documents in the apps sandbox. Then find the path to that location (if there is an existing pList file, it will overwrite), and use:

[myDictionary writeToFile:path atomically:YES];

Write how far you are in the process, and maybe some code / error-message...

Cloche answered 21/10, 2010 at 8:49 Comment(1)
here's my code: NSMutableDictionary *nameDetails=[[NSMutableDictionary alloc] init]; [nameDetails setValue:username forKey:USERNAME_KEY]; [nameDetails setValue:password forKey:PASSWORD_KEY]; NSString *loginDetails = [[NSBundle mainBundle] pathForResource:@"Login" ofType:@"plist"]; [nameDetails writeToFile:loginDetails atomically:YES]; savedStock = [NSMutableDictionary dictionaryWithContentsOfFile:loginDetails];Quianaquibble
A
4
[yourDict writeToFile:filePath atomically:YES];

Note that dictionary must contain plist objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary). And dictionary keys must be NSString objects

Amputate answered 21/10, 2010 at 6:12 Comment(2)
here's my code [nameDetails writeToFile:loginDetails atomically:YES];Quianaquibble
What is loginDetails path? And are you sure that your dictionary conforms the conditions for its contents types?Amputate
G
2

The issue is that your application is attempting to write the plist into its own bundle. Your plist should be stored in the User's Library. However, if you are storing a user's credentials for a service, you really aught to use the Mac OS X Keychain services to store that information.

Germ answered 8/12, 2010 at 14:41 Comment(0)
Q
1
NSMutableDictionary *nameDetails=[[NSMutableDictionary alloc] init]; 
[nameDetails setValue:username forKey:USERNAME_KEY]; 
[nameDetails setValue:password forKey:PASSWORD_KEY]; 

NSString *loginDetails = [[NSBundle mainBundle] pathForResource:@"Login" ofType:@"plist"]; 
[nameDetails writeToFile:loginDetails atomically:YES];

savedStock = [NSMutableDictionary dictionaryWithContentsOfFile:loginDetails];
Quianaquibble answered 22/10, 2010 at 11:19 Comment(0)
B
1
NSString *pathToPlist = [[NSBundle mainBundle] bundlePath];
pathToPlist = [pathToPlist stringByDeletingLastPathComponent];
pathToPlist = [pathToPlist stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@", pathToPlist; //*
pathToPlist = [pathToPlist stringByAppendingPathComponent:@"Login.plist"];
NSDictionary *nameDetails = @{@"name": @"Albert", @"password": @"emc2"};
[nameDetails writeToFile:pathToPlist atomically:YES];
  • We are logging the path to console, in order to be able to find it in the Finder. Copy the path from the console to the clipboard. Then in the Finder chooose "Go To Folder..." from the Go menu. Paste the path into the dialog box and the folder where the plist is will be opened. Then you can examine it and make sure that everything works as it should.
Berl answered 4/12, 2013 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.