如何成功写入plist?

发布于 2024-12-02 03:52:36 字数 1988 浏览 0 评论 0原文

在浏览了许多教程、其他人的问题并自己尝试后,我在将文件写入 plist 时遇到了问题。我可以毫无问题地读取 plist,但无法更新它。下面是我如何将数据写入 plist 的代码。如果我犯了任何错误,请纠正我。

    NSString *path = [[NSBundle mainBundle] pathForResource:@"EventAddress" ofType:@"plist"]; 
    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSArray* allmyData = [myDictionary  allValues];

    // creates and array to store only the event details
    NSMutableArray *data = [[NSMutableArray alloc] initWithArray:allmyData];
    [data addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:tfAddress.text, @"Address", tvEvents.text, @"Events", nil]];
    [myDictionary setValue:data forKey:@"Whampo"];
    BOOL flag = [myDictionary writeToFile:path atomically:YES];
    if (flag){
        NSLog(@"write to plist success");
    }
    NSLog(@"%@", myDictionary);
    [myDictionary release];

路径是正确的,文件存在,我在textView和textField中的值在数组中,但是当涉及到writeToFile时,它不会反映位于文档目录中的文件。

编辑01:

我在网上找到了这个,与Nekto的建议非常相似。但我正在考虑如何用他的代码来实现我的代码。我认为这很简单,但我似乎不知道如何做。

NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *plistDirectory = [NSString stringWithFormat:@"%@/Enterprise",documentDirectory];

NSString *mPath = [plistDirectory stringByAppendingPathComponent:@"Downloads.plist"]; 

[mDownloadsArray writeToFile:mPath atomically:YES];

iphonesdk.blogspot 取自该网站。

编辑02:

我使用了Nekto的建议,效果很好。但我很好奇为什么它返回 DocumentsEventAddress.plist 而不是 EventAddress.plist。我的假设是因为 NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *plistPath = [rootPath stringByAppendingString:@"EventAddress.plist"];

rootPath 返回 Document 的地方是这样吗?

I am having trouble writing my file into the plist after going through many tutorials, other people's problems and attempting it for myself. I can read the plist with no problems but I cant update it. Below are my codes on how I am writing my data into the plist. Correct me if I made any mistake.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"EventAddress" ofType:@"plist"]; 
    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSArray* allmyData = [myDictionary  allValues];

    // creates and array to store only the event details
    NSMutableArray *data = [[NSMutableArray alloc] initWithArray:allmyData];
    [data addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:tfAddress.text, @"Address", tvEvents.text, @"Events", nil]];
    [myDictionary setValue:data forKey:@"Whampo"];
    BOOL flag = [myDictionary writeToFile:path atomically:YES];
    if (flag){
        NSLog(@"write to plist success");
    }
    NSLog(@"%@", myDictionary);
    [myDictionary release];

The path is correct, the file exists, my values in the textView and textField are in the array, but when it comes to the writeToFile, it does not reflect on the file located at the document directory.

EDIT 01:

I found this online, very similar to Nekto's suggestion. But I am thinking on how to implement my code with his. I think its pretty simple, but I cant seem to figure out how to.

NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *plistDirectory = [NSString stringWithFormat:@"%@/Enterprise",documentDirectory];

NSString *mPath = [plistDirectory stringByAppendingPathComponent:@"Downloads.plist"]; 

[mDownloadsArray writeToFile:mPath atomically:YES];

iphonesdk.blogspot taken from that site.

EDIT 02:

I used Nekto's suggestion and it worked well. But I am curious why it is returning DocumentsEventAddress.plist rather than EventAddress.plist. My assumption is because of the
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingString:@"EventAddress.plist"];

Where rootPath is returning Document is that right?

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

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

发布评论

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

评论(3

热风软妹 2024-12-09 03:52:36

我正在以这种方式写入文件:

NSString *errorDesc = nil;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingString:TEMPLATES_PATH];
NSDictionary *dict = [NSDictionary dictionaryWithObject:templates forKey:@"templates"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
if (plistData)
{
    [plistData writeToFile:plistPath atomically:YES];
}else
{
    NSLog(@"[Error] Application Did Enter Background {saving file error}: %@", errorDesc);
    [errorDesc release];
}

请务必将文件保存在应用程序文档目录中。

I'm writing to file in such way:

NSString *errorDesc = nil;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingString:TEMPLATES_PATH];
NSDictionary *dict = [NSDictionary dictionaryWithObject:templates forKey:@"templates"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
if (plistData)
{
    [plistData writeToFile:plistPath atomically:YES];
}else
{
    NSLog(@"[Error] Application Did Enter Background {saving file error}: %@", errorDesc);
    [errorDesc release];
}

Be sure to save file in app documents directory.

妞丶爷亲个 2024-12-09 03:52:36

我不相信你可以写入资源。只能写入文档目录中的文件。

I don't believe you can write to a resource. Only files in the documents directory can be written to.

兮子 2024-12-09 03:52:36

您无法写入位于捆绑包中的文件。
该应用程序包是只读的。

You can't write to files that are located in you Bundle.
The app bundle is read only.

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