如何在应用程序本身内存储可可应用程序的键值数据?

发布于 2024-12-17 23:09:28 字数 119 浏览 2 评论 0原文

我有一个包含用户数据的信息字典。目前,它被写入与应用程序同一目录中的 xml 文件中。不过,我非常确定 cocoa 允许我将此 xml 文件写入应用程序包或应用程序内的某些资源目录中。

有人可以教我怎么做吗?

I have an info dictionary that contains user data. Currently, it is written to an xml file in the same directory as the app. However, I'm pretty certain cocoa allows me to write this xml file into the application bundle or some Resources directory within the app.

Can someone teach me how to do this?

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

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

发布评论

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

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-12-24 23:09:28

您可能希望在 NSDocumentDirectory 中使用 NSFileManagercreateFileAtPath:contents:attributes: (相对于您的包,这是 /Documents< /code>) 与 xml 文件的 NSData
像这样的东西:

NSString *myFileName = @"SOMEFILE.xml";
NSFileManager *fileManager = [NSFileManager defaultManager];

// This will give the absolute path of the Documents directory for your App
NSString *docsDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

// This will join the Documents directory path and the file name to make a single absolute path (exactly like os.path.join, if you python)
NSString *xmlWritePath = [docsDirPath stringByAppendingPathComponent:myFileName];

// Replace this next line with something to turn your XML into an NSData
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:@"http://someurl.com/mydoc.xml"];

// Write the file at xmlWritePath and put xmlData in the file.
BOOL created = [fileManager createFileAtPath:xmlWritePath contents:xmlData attributes:nil];
if (created) {
    NSLog(@"File created successfully!");
} else {
    NSLog(@"File creation FAILED!");
}

// Only necessary if you are NOT using ARC and you alloc'd the NSData above:
[xmlData release], xmlData = nil;

一些参考文献:

NSFileManager 参考文档
NSData 参考文档


编辑

为了回应您的评论,这将是 NSUserDefaults 在应用程序运行之间保存可序列化数据的典型用法:

// Some data that you would want to replace with your own XML / Dict / Array / etc
NSMutableDictionary *nodeDict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"object1", @"key1", nil];
NSMutableDictionary *nodeDict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"object2", @"key2", nil];
NSArray *nodes = [NSArray arrayWithObjects:nodeDict1, nodeDict2, nil];

// Save the object in standardUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:nodes forKey:@"XMLNODELIST"];
[[NSUserDefaults standardUserDefaults] synchronize];

检索保存的值(下次启动应用程序时,或从应用程序的其他部分等):

NSArray *xmlNodeList = [[NSUserDefaults standardUserDefaults] arrayForKey:@"XMLNODELIST"];

NSUserDefaults 参考文档

You would want to use NSFileManager to createFileAtPath:contents:attributes: in the NSDocumentDirectory (relative to your bundle this is /Documents) with the NSData of your xml file.
Something like this:

NSString *myFileName = @"SOMEFILE.xml";
NSFileManager *fileManager = [NSFileManager defaultManager];

// This will give the absolute path of the Documents directory for your App
NSString *docsDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

// This will join the Documents directory path and the file name to make a single absolute path (exactly like os.path.join, if you python)
NSString *xmlWritePath = [docsDirPath stringByAppendingPathComponent:myFileName];

// Replace this next line with something to turn your XML into an NSData
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:@"http://someurl.com/mydoc.xml"];

// Write the file at xmlWritePath and put xmlData in the file.
BOOL created = [fileManager createFileAtPath:xmlWritePath contents:xmlData attributes:nil];
if (created) {
    NSLog(@"File created successfully!");
} else {
    NSLog(@"File creation FAILED!");
}

// Only necessary if you are NOT using ARC and you alloc'd the NSData above:
[xmlData release], xmlData = nil;

Some references:

NSFileManager Reference Docs
NSData Reference Docs


Edit

In response to your comments, this would be typical usage of NSUserDefaults to save serializable data between App runs:

// Some data that you would want to replace with your own XML / Dict / Array / etc
NSMutableDictionary *nodeDict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"object1", @"key1", nil];
NSMutableDictionary *nodeDict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"object2", @"key2", nil];
NSArray *nodes = [NSArray arrayWithObjects:nodeDict1, nodeDict2, nil];

// Save the object in standardUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:nodes forKey:@"XMLNODELIST"];
[[NSUserDefaults standardUserDefaults] synchronize];

To retrieve a saved value (next time the App is launched, or from another part of the App, etc):

NSArray *xmlNodeList = [[NSUserDefaults standardUserDefaults] arrayForKey:@"XMLNODELIST"];

NSUserDefaults Reference Docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文