gpt4 book ai didi

ios - 以编程方式更新在模拟器中显示但不在设备中显示的子 plist

转载 作者:行者123 更新时间:2023-11-29 03:34:07 25 4
gpt4 key购买 nike

我第一次实现设置包,我是通过在 Settings.bundle 中创建 Root.plist 和 Child.plist 来完成的,并在这些 plist 中包含一些字段。我需要根据 Web 服务更新 Child.plist 的字段(添加或删除),并且我已在 appdelegate 中为此编写了代码。它在模拟器中工作正常(所有字段都会相应更新),但在设备中不起作用。任何人都可以告诉我这可能是什么原因吗?

将 plist 复制到文档目录并更新它的代码(根据 asnwer)

NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Settings" ofType:@"bundle"];
NSString *path = [bundle stringByAppendingPathComponent:@"Category.plist"];

NSMutableDictionary *dictOfPlist = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *dictArray = [dictOfPlist valueForKey:@"PreferenceSpecifiers"];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path1 = [documentsDirectory stringByAppendingPathComponent:@"Child.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path1])
{
[fileManager copyItemAtPath:path toPath: path1 error:&error];
}
for (some condition)
{
// creating dictionary
// add dictionary to array dictArray
}
[dictOfPlist setObject:dictArray forKey:@"PreferenceSpecifiers"];
[dictOfPlist writeToFile:path1 atomically:YES];

最佳答案

我认为您在编写 PLIST 时遇到问题。您需要将资源包中的 PLIST 复制到文档目录中,并修改此文件夹中的 PLIST。

为此,您可以使用这段代码:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}

看看here .

关于ios - 以编程方式更新在模拟器中显示但不在设备中显示的子 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19403552/

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