如何处理嵌套属性列表

时间:2013-07-25 11:12:58

标签: ios nsmutablearray plist nsdictionary nsmutabledictionary

美好的一天!

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

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

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

这是我尝试过的代码:

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

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

2 个答案:

答案 0 :(得分:1)

您首先从plist中检索Dictionary,然后编写一个Array。其中一个是错的。 根据你想要尝试这样的事情:

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

答案 1 :(得分: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的sroot。但是在根部你想拥有NSArray。这就是为什么,这个:

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

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