gpt4 book ai didi

iphone - 在不删除之前数据的情况下将数据添加到 plist

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

正如您从标题中看到的那样,我基本上想更新一个 plist。实际上,应用程序用户会在数据字段中键入他的数据,我会将它们保存在 plist 中。他可以添加任意数量的对象。我已经为该员工尝试了大量不同的代码,但没有一个有效。首先看一下我的plist(我给你看xml代码,这样你就可以理解了)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>TEST</string>
<key>code</key>
<string>TEST</string>
<key>longitude</key>
<integer>22</integer>
<key>latitude</key>
<integer>10</integer>
</dict>
</array>
</plist>

那只是一个简单的条目。现在这是我用过的最新代码,但我一点运气都没有

NSMutableDictionary *newBuilding = [NSMutableDictionary alloc];
[newBuilding setValue:@"Test1" forKey:@"name"];
[newBuilding setValue:@"Test2" forKey:@"code"];
[newBuilding setValue:@"10" forKey:@"latitude"];
[newBuilding setValue:@"20" forKey:@"longitude"];

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsPath = [paths objectAtIndex:0];

NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"BuildingsInfo.plist"];

NSMutableArray *newArray = [NSMutableArray arrayWithContentsOfFile:plistPath];

if (nil == newArray) {
newArray = [[NSMutableArray alloc] initWithCapacity:0];
}

[newArray addObject:newBuilding];
[newArray writeToFile:plistPath atomically: YES];

}

任何人都可以发现任何错误吗?我已经写了很多次了,我检查了每个标记,但没有发现错误。我不明白为什么我不想工作!

非常感谢各位

最佳答案

就其值(value)而言,您从未初始化 newBuilding

关于iphone - 在不删除之前数据的情况下将数据添加到 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9541847/

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