【问题标题】:How to save data on the next index of plist如何将数据保存在plist的下一个索引上
【发布时间】:2015-11-26 17:37:50
【问题描述】:

我是 iOS 编程的新手,并且开始接触它。我在网上搜索了一些 plist 帮助,得到了一些东西并理解并使用了它,但现在我陷入了困境。我已经搜索了很多这个问题。但我只是无法为我找到正确的答案。

问题: 我的 UI 只有 2 个文本字段和 1 个保存按钮。 1 个文本字段采用字符串,而另一个采用数字 (int) 作为输入。

我的 plist 有 1 个字典项,其中有 1 个字符串项和 1 个 int 项。就是这样。

我从用户那里获取 2 个 UITextView 的输入,并通过保存按钮将它们保存到这个 plist 中。

问题是每当我输入新值并按下保存按钮时,它都会覆盖旧的 plist 数据。

我发现我需要阅读字典,将新值附加到它,然后将其保存回来以获得我想要的输出。但我无法抓住这个概念并将其放入代码中。一些带解释的代码真的很有帮助。

我的保存按钮是这样工作的:

-(IBAction)saveit
{
    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];

    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];

    // set the variables to the values in the UITextField text1 n text2 respectively

   self.personName = text1.text;
    num = (int)text2.text;    


    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, num, nil] forKeys:[NSArray arrayWithObjects: @"name", @"phone", nil]];

    NSString *error = nil;

    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData)
    {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
    }
    else
    {
        NSLog(@"Error in saveData: %@", error);
       // [error release];
    }

}

这段代码工作得很好,只是它写完了新值。请帮忙。

【问题讨论】:

    标签: iphone xcode plist


    【解决方案1】:

    您的 plist 将返回一个字典。将该字典检索为 NSMutableDictionary 并将您的新密钥对值添加到该字典,然后保存它。

    【讨论】:

      【解决方案2】:

      像这样将数据保存为字典数组

      // get paths from root direcory
      NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsPath = [paths objectAtIndex:0];
      NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];
      
      //Load the original file
      NSMutableArray *arr;
      if([[NSFileManager defaultManager] fileExistsAtPath:plistPath])   
           //File exist load
           arr = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
      else
          //File does not exist create
          arr = [[NSMutableArray alloc] init];
      
      //Create dictionary
      NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, num, nil] forKeys:[NSArray arrayWithObjects: @"name", @"phone", nil]];
      
      //Append to arr
      [arr addObject:plistDict];
      
      //Save to file
      [arr writeToFile:plistPath atomically:YES];
      

      【讨论】:

      • 做到了,但我认为它没有在 arr 中放入任何东西(第 9 行).. 并且一旦编译它就会给出 SIGABRT 错误。
      • 请从设备中删除您的旧应用并重新运行
      • 那是奥马尔。还是行不通。附言感谢您的宝贵时间!
      • 我真的相信你的代码是正确的,但不知道为什么它会给出 SIGABRT 错误:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多