【问题标题】:writing to a plist in documents directory Xcode写入文档目录 Xcode 中的 plist
【发布时间】:2016-09-19 11:19:21
【问题描述】:

我有一个应用程序,我将一个 plist 从主包复制到我用来填充分段 UITableview 的文档目录。这一切运作良好。我想在用户输入时更改 plist 中的一些信息(部分的标题名称)和(单元格的详细文本)。我还想添加其他部分,复制那里的部分并在用户输入信息时输入信息。这是plist

我已经查找了这方面的信息并尝试了各种方法,但似乎没有任何效果。有人可以告诉我如何更改标题字符串以及如何添加详细文本并添加带有标题的全新部分和带有项目的行吗?

这是我用来尝试更新头文件但没有运气的代码

 NSArray *documentPaths15 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory18 = [documentPaths15 objectAtIndex:0];
    NSString *path10 = [documentsDirectory18  stringByAppendingPathComponent:@"TableDataMainView.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSMutableDictionary *data1;

    if ([fileManager fileExistsAtPath: path10])
    {
        data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: path10];  // if file exist at path initialise your dictionary with its data
        NSLog(@"file exists");
    }
    else
    {
        // If the file doesn’t exist, create an empty dictionary
        data1 = [[NSMutableDictionary alloc] init];
        NSLog(@"file doesnt exist");
    }

    //To insert the data into the plist

    [data1 setObject:[NSString stringWithString:yourName] forKey:@"header"];
    [data1 writeToFile: path10 atomically:YES];

任何帮助将不胜感激。

【问题讨论】:

  • plist 通常用于管理持久化数据存储索引文件。所以你可以读一次然后写回来。但您不能以编程方式创建新条目。如果您的数据是动态的,请使用核心数据实体。
  • 我去了appcoda“核心数据简介”并做了应该做的事情,但我正在使用 - (void)userSwiped:(UIGestureRecognizer *)sender
  • 我已经去了appcoda“核心数据简介”并完成了应该做的事情,但我正在使用 - (void)userSwiped:(UIGestureRecognizer *)sender 而不是按钮操作来保存信息.但我不断收到以下错误 -[AppDelegate managedObjectContext]: unrecognized selector sent to instance 0x14fd262f0
  • 核心数据是一个浩瀚的话题,慢慢来学习吧。我建议您从developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…raywenderlich.com/14742/… 开始。 HTH。

标签: uitableview ios7 plist nsdocumentdirectory


【解决方案1】:

截至目前.. plist 通常用来管理持久化数据存储索引文件。所以你可以读一次然后写回来。但您不能以编程方式创建新条目。如果您的数据是动态的,请使用核心数据实体。

【讨论】:

  • 搞定了感谢所有帮助现在只需要更改单元格的 textLabel 和 detailTextLabel 文本颜色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
相关资源
最近更新 更多