2012-03-12 49 views
0

我试过这段代码将我的数据复制到plist,但它在追加.can的情况下不工作。任何一个都可以告诉我一个附加数据的好方法吗?
NSMutableDictionary * nameDictionary = [NSMutableDictionary dictionary];任何人可以帮助我将数据附加到plist文件?

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"]; 
[nameDictionary setValue:nameEntered.text forKey:@"name"]; 
[nameDictionary setValue:company.text forKey:@"company"]; 
[nameDictionary setValue:designation.text forKey:@"designation"]; 

// create dictionary with values in UITextFields 
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
if (plist == nil) plist = [NSMutableArray array]; 
[plist addObject:nameDictionary]; 
[plist writeToFile:plistPath atomically:YES]; 
UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:@"succsefully created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[tellErr show]; 
[email protected]""; 
[email protected]""; 
[email protected]""; 
+0

当你说它不工作时,你是否看到崩溃或数组没有字典,你预计将被添加到最后?从我看到的,你上面的代码看起来很好。 – Sam 2012-03-12 14:06:16

+0

当我试图插入它第一次它从plist中删除整个数据,或者如果我正在查看列表,然后去插入它崩溃 – ashwin19 2012-03-12 14:15:52

+0

如果您的问题得到解答,请将其标记为绿色支票 – 2012-03-13 12:55:31

回答

0

我会去:

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"]; 
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; 
if (plist == nil) plist = [[NSMutableDictionary alloc] init]; 
[plist setObject:nameEntered.text forKey:@"name"]; 
[plist setObject:company.text forKey:@"company"]; 
[plist setObject:designation.text forKey:@"designation"]; 
[plist writeToFile:plistPath atomically:YES]; 

...等...

当然,这取决于正是你想要达到的目标。

+0

但这会覆盖旧的数据。我想要将新输入的数据追加到旧数据中 – ashwin19 2012-03-12 14:17:02

+0

这不会覆盖旧数据。但是,它有一个错误,我会在一分钟左右内解决。 – 2012-03-12 22:31:02

+0

好的,错误是固定的。 dictionayWithContentsOfFile分配并初始化字典并将文件内容读入字典。然后,plist值在字典中。然后添加3个新值,然后将完整的字典写入相同的文件名。应该做的伎俩。 – 2012-03-12 22:43:16

0

您无法写入iOS中的应用程序主包。您必须将属性列表文件复制到可写目录;那么你可以阅读它,修改它,并重写它。

+0

你是告诉我从文档目录或类似的文件夹中取出plist文件吗? – ashwin19 2012-03-13 04:25:39

+0

对,上面的代码段读取plist,大概预先填充主包中的数据。但是,您不能将修改后的文件版本保存回主包。您必须将其保存到文档目录等。出于此原因,应用程序通常会在首次启动时将需要修改的资源复制到应用程序支持目录或文档目录。 – FluffulousChimp 2012-03-13 10:40:33

0

好的老兄,这里是你需要的代码。我用这在现有的iOS程序

第一次检查,看是否有在字典中的任何信息,开始与

static BOOL dicIsNULL = YES; 

.... 

//initialize the variable 
dicIsNULL = YES; 


//READ FILE 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"listNew.plist"]; 

     dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath]; 

     //Check if this dic has values 
     for (id key in dictionary) 
     { 
      //NSLog(@"listNew.plist file has values"); 
      dicIsNULL = NO; 
      break; 
     } 

现在做这取决于已经被创造或不dictonary以下。在字典中添加你的值,这只是一个示例代码。

if (dicIsNULL == YES) 
     { 
      //NSLog(@"dictionary is NULL"); 

      dictionary = [[NSMutableDictionary alloc] init]; 
      dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:tempArray, infoKeyStr, textTitleField1.text, titleKeyStr, userSelectedCatStr, catKeyStr, nil]; 

     } 

     if (dicIsNULL == NO) 
     { 
      //NSLog(@"dictionary is NOT null"); 

      dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath]; 
      [dictionary setObject: tempArray forKey: infoKeyStr]; 
      [dictionary setObject: textTitleField1.text forKey: titleKeyStr]; 
      [dictionary setObject: userSelectedCatStr forKey: catKeyStr]; 
     } 

希望这可以帮助你。

相关问题