gpt4 book ai didi

iphone - 修改plist不起作用

转载 作者:行者123 更新时间:2023-12-01 18:02:50 25 4
gpt4 key购买 nike

我必须在存储在bundle中的plist文件中修改BOOL值。我能够访问我必须修改的字典。从nslogging中我可以看到dict已使用新值更新,但是问题是当我检查时捆绑中的plist未被修改。任何不更新plist的线索

  NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TopicsList.plist"])
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableArray* dictarrays = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableDictionary *dict=[dictarrays objectAtIndex:indexPath.row];
NSLog(@"%@ ",dict );

[dict setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];

NSLog(@"%@ ",dict );
[dict writeToFile:plistPath atomically:NO];
NSLog(@"%@ ",dict );
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:&error];
}
}

最佳答案

plist是您的一部分资源吗?不知道我们是否可以在那里编辑plist。将plist复制到应用程序的Documents文件夹中,然后在此处进行更新。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"TopicsList.plist"];

success = [fileManager fileExistsAtPath:plistPath];
if(!success){
//file does not exist. So look into mainBundle
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TopicsList.plist"];
success = [fileManager copyItemAtPath:defaultPath toPath:plistPath error:&error];
}

现在,您需要对plist进行任何更改或从plist读取数据,都可以从Documents文件夹中的副本中读取数据。

关于iphone - 修改plist不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6368752/

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