【问题标题】:Saving data to plist per object or person将数据保存到每个对象或人的 plist
【发布时间】:2023-03-10 19:33:01
【问题描述】:

我正在尝试将每个人的数据保存到我的应用程序中的 plist,但它会将其保存给每个人,因此例如,如果我有一个眼睛颜色字段并且我在 John Doe 的文本字段中输入 Brown,它会保存 Brown对于其他所有人,有没有办法在应用程序中保存每个人的信息,而不是使用 plist?我没有运气尝试:

-(NSString *)pathofFile{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsfolder = [paths objectAtIndex:0];
    return [docsfolder stringByAppendingFormat:@"data.plist"];
}

这是我认为加载的代码:

- (void)viewDidLoad {
    [self loadPerson];
    [super viewDidLoad];

    NSString *filepath = [self pathofFile];
    if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
        NSArray *array = [[NSArray alloc]initWithContentsOfFile:filepath];
        Drinfo.text = [array objectAtIndex:0];
        [array release];
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name: UIApplicationWillTerminateNotification object:app];

这是我的 saveperson 方法的一部分:

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:self.Drinfo.text];
[array writeToFile:[self pathofFile] atomically:YES];
[array release];

【问题讨论】:

  • 一些代码会很有帮助。听起来您在构建数据结构以进入您的 plist 的代码中有一个相当基本的错误,如果我们能看到它,我们可能会很容易地修复它。

标签: ios iphone object save plist


【解决方案1】:

您可以使用plist,只需为plist 中的每个人创建一个NSDictionary,然后将颜色字符串添加到其中。步骤类似于

  1. 从文件中打开plist,将其设置为NSDictionary,并确保其为mutable copy
  2. 创建一个新的NSMutableDictionary 并向其中添加任何属性,例如眼睛颜色。
  3. mutable dictionary 添加到您的 plist dictionary,并将密钥设置为人名
  4. 保存plist

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多