0

再会!

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

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

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

这是我尝试的代码:

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

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

4

2 回答 2

1

您首先从 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];
于 2013-07-25T11:38:01.067 回答
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 只是我传递的变量。
希望能帮助到你!!

于 2013-07-25T11:55:28.313 回答