【问题标题】:How to handle nested property list如何处理嵌套的属性列表
【发布时间】:2013-07-25 11:12:58
【问题描述】:

美好的一天!

我在 iOS 中执行嵌套属性列表时遇到问题。

因此,有两个UITextFields 将接受随机值,然后将其保存到属性列表中。问题是,当我输入一个 second 值时,它会覆盖我的属性列表中的 first 值。

如何处理或编写嵌套属性列表?

这是我尝试的代码:

- (IBAction)writeToPlist:(id)sender
{
    NSLog(@"Write.");

    NSString *finalPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Data.plist"];

    NSMutableDictionary *fruitDictionary = [[NSMutableDictionary alloc] init];

    NSString *fruitName = [[NSString alloc] initWithString:[fruitNameField text]];
    NSString *fruitDescription = [[NSString alloc] initWithString:[fruitDescriptionField text]];

    NSDictionary *fruitDetail = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:fruitName, fruitDescription, nil]
                                                            forKeys:[NSArray arrayWithObjects:@"Fruit", @"Description", nil]];

    NSMutableDictionary *fruitPlist = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
    NSMutableArray *fruitArray = [fruitPlist objectForKey:fruitName];

    if (fruitArray == nil) {
        fruitArray = [[NSMutableArray alloc] init];
    }

    [fruitDictionary setObject:fruitDetail forKey:fruitName];

    [fruitArray addObject:fruitDictionary];
    [fruitArray writeToFile:finalPath atomically:YES];

    [[self presentingViewController] dismissViewControllerAnimated:YES
                                                        completion:nil];
}

输出是:

<plist version="1.0">
<array>
    <dict>
        <key>Apple</key>
        <dict>
            <key>Description</key>
            <string>Red</string>
            <key>Fruit</key>
            <string>Apple</string>
        </dict>
    </dict>
</array>
</plist>

我想要发生的是:

<plist version="1.0">
<array>
    <dict>
        <key>Apple</key>
        <dict>
            <key>Description</key>
            <string>Red</string>
            <key>Fruit</key>
            <string>Apple</string>
        </dict>
    </dict>
    <dict>
        <key>Banana</key>
        <dict>
            <key>Description</key>
            <string>Yellow</string>
            <key>Fruit</key>
            <string>Banana</string>
        </dict>
    </dict>
</array>
</plist>

顺便问一下,我的代码是否可以接受?我的意思是,有什么办法可以缩短它吗?

【问题讨论】:

    标签: ios nsmutablearray plist nsdictionary nsmutabledictionary


    【解决方案1】:

    所以我玩弄了你的代码并找出了一些问题。

    这是我编写和测试的内容。它正在工作。

      NSString *finalPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"Data.plist"];
      NSMutableDictionary *fruitDictionary = [[NSMutableDictionary alloc] init];
      NSString *fruitName = fName;
      NSString *fruitDescription = fDetail;
      NSDictionary *fruitDetail = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:fruitName, fruitDescription, nil]
                                                              forKeys:[NSArray arrayWithObjects:@"Fruit", @"Description", nil]];
      NSMutableArray *fruitPlist = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
    
    
    if (fruitPlist == nil) {
            fruitPlist = [[NSMutableArray alloc] init];
        }
      [fruitDictionary setObject:fruitDetail forKey:fruitName];
      [fruitPlist addObject:fruitDictionary];
      [fruitPlist writeToFile:finalPath atomically:YES];
    

    说明: 您将代码中的内部字典作为 plist 的根目录。但是,您希望在根目录下拥有 NSArray。这就是为什么,这个:

    NSMutableArray *fruitPlist = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
    

    在我的代码中,fName 和 fDetail 只是我传递的变量。
    希望对你有帮助!!

    【讨论】:

    • Data.plist 不会出现在目录中。
    • 哎呀!!我有点忘了第一次添加数组。查看我的编辑。
    【解决方案2】:

    您首先从 plist 中检索字典,然后编写一个数组。 2个之一是错误的。 基于此,您可能想尝试这样的事情:

        NSMutableArray *fruitPlist = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
    
    NSDictionary *newFruit;
    
    for (NSDictionary *fruit in fruitPlist) {
        if ([fruit objectForKey:fruitName]) newFruit = fruit;
    }
    
    if (!newFruit) {
        newFruit = [[NSDictionary alloc] init];
        [fruitPlist addObject:newFruit];
    }
    else {
        NSInteger index = [fruitPlist indexOfObject:newFruit];
    
        // modify newFruit as you wish
    
        [fruitPlist replaceObjectAtIndex:index withObject:newFruit];
    
    }
    
    [fruitPlist writeToFile:finalPath atomically:YES];
    

    【讨论】:

    • 你通常在 else 语句中放什么,你有什么评论? 编辑: 它不会生成数据 plist。
    • if else 有一种方法可以处理已经创建但您想要修改(在 else 语句中)的新水果(在 if 语句中)。跨度>
    猜你喜欢
    • 2014-09-17
    • 2022-08-16
    • 1970-01-01
    • 2016-04-22
    • 2018-07-19
    • 2011-05-18
    • 2019-12-21
    • 1970-01-01
    • 2020-08-28
    相关资源
    最近更新 更多