将每个对象或人的数据保存到 plist

发布于 2024-09-13 00:40:41 字数 1241 浏览 3 评论 0原文

我试图将数据保存到我的应用程序中的每个人的 plist 中,但它会将其保存给每个人,因此,例如,如果我有一个眼睛颜色字段,并且我在 John Doe 的文本字段中输入棕色,它会为其他人保存棕色还有,有没有办法在应用程序中保存每个人的信息,而不是使用 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];

I am trying to save data to the plist in my app per person, but it save it to everyone, so for example if I have a field for eye color and I enter Brown in the textfield for John Doe, it save Brown for everyone else as well, is there a way to save this info per person in the app instead using plist? I have had no luck trying:

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

Here is the code I have in my view did load:

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

and here is what I have as part of my saveperson method:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-09-20 00:40:41

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

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

You could use a plist just create a NSDictionary for each person inside the plist and add the colour string to that. The steps would be something like

  1. Open up the plist from file, set it to be a NSDictionary and make sure its a mutable copy.
  2. Create a new NSMutableDictionary and add any attributes to that e.g. eye colour.
  3. Add that mutable dictionary to your plist dictionary with the key set to the persons name
  4. Save the plist
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文