gpt4 book ai didi

ios - 如何处理嵌套的属性列表

转载 作者:行者123 更新时间:2023-11-28 22:29:33 24 4
gpt4 key购买 nike

美好的一天!

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

因此,有两个 UITextField 将接受一个随机值,然后将其保存到属性列表中。问题是,当我输入 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>

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

最佳答案

所以我研究了您的代码并找出了一些问题。

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

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

关于ios - 如何处理嵌套的属性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17856410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com