如何将 NSMutableDictionary 写入 Plist

发布于 2024-09-28 02:33:50 字数 73 浏览 3 评论 0原文

任何人都可以帮助我解决如何将 NSMutableDictionary 写入 plist 的问题....

提前致谢..

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

thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪痕残 2024-10-05 02:33:50

确保您要写入的 pList 文件位于合法位置以对其进行编辑,例如应用程序沙箱中的文档。
然后找到该位置的路径(如果存在现有的 pList 文件,它将覆盖),并使用:

[myDictionary writeToFile:path atomically:YES];

写下您在该过程中的进度,也许还有一些代码/错误消息...

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...

夏花。依旧 2024-10-05 02:33:50
[yourDict writeToFile:filePath atomically:YES];

请注意,字典必须包含 plist 对象(NSDataNSDateNSNumberNSStringNSArray 的实例NSDictionary)。并且字典键必须是 NSString 对象

[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

夜唯美灬不弃 2024-10-05 02:33:50

问题是您的应用程序正在尝试将 plist 写入自己的包中。您的 plist 应存储在用户库中。但是,如果您要存储用户的服务凭据,则确实可以使用 Mac OS X 钥匙串服务来存储该信息。

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.

我乃一代侩神 2024-10-05 02:33:50
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];
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];
花落人断肠 2024-10-05 02:33:50
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];
  • 我们正在记录控制台的路径,以便能够在 Finder 中找到它。将路径从控制台复制到剪贴板。然后在 Finder 中,从“转到”菜单中选择“转到文件夹...”。将路径粘贴到对话框中,将打开 plist 所在的文件夹。然后您可以检查它并确保一切正常。
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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文