2013-07-25 94 views
0

美好的一天!如何处理嵌套属性列表

我在iOS中做一个嵌套的属性列表有问题。

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

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

这里是我尝试代码:

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

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

回答

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只是我传递的变量。
希望它有帮助!

+0

Data.plist不会出现在目录中。 – Jahm

+0

OOPS !!我有点忘了第一次添加数组。看我的编辑。 –

1

您首先从plist中检索字典,然后编写一个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]; 
+0

你通常放在else语句中,你有什么评论? **编辑:**它不会生成数据plist。 – Jahm

+0

如果还有一种方法可以使用已经创建但您想要修改的水果(在if语句中)的新水果(在else语句中)。 – cescofry