如何成功写入plist? [英] How to write to plist successfully?

查看:105
本文介绍了如何成功写入plist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在完成许多教程,其他人的问题并亲自尝试后,我无法将文件写入plist。我可以毫无问题地阅读plist,但我无法更新它。以下是关于我如何将数据写入plist的代码。如果我犯了任何错误,请纠正我。

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];

路径正确,文件存在,textView和textField中的值都在数组中,但是当涉及到writeToFile时,它并没有反映在文档目录中的文件。

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.

编辑01:

我在网上找到了这个,与Nekto的建议非常相似。但我正在考虑如何用他的代码实现我的代码。我认为它非常简单,但我似乎无法弄清楚如何。

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

编辑02:

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

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"];

其中rootPath返回文件是吗?

Where rootPath is returning Document is that right?

推荐答案

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

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];
}

请务必将文件保存在app documents目录中。

Be sure to save file in app documents directory.

这篇关于如何成功写入plist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆