gpt4 book ai didi

xcode - 将数据添加到 plist 文件的问题

转载 作者:行者123 更新时间:2023-12-04 06:09:18 29 4
gpt4 key购买 nike

我一直在尝试将数据写回到我的包中的预定义 plist 文件 (data.plist)。使用下面的代码,我调用例程“dictionaryFromPlist”来打开文件,然后调用“writeDictionaryToPlist”来写入 plist 文件。但是,没有数据被添加到 plist 文件中。

NSDictionary *dict = [self dictionaryFromPlist];

NSString *key = @"Reports";
NSString *value = @"TestingTesting";
[dict setValue:value forKey:key];

[self writeDictionaryToPlist:dict];


- (NSMutableDictionary*)dictionaryFromPlist {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc]
initWithContentsOfFile:filePath];
return [propertyListValues autorelease];
}

- (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
NSString *filePath = @"data.plist";
BOOL result = [plistDict writeToFile:filePath atomically:YES];
return result;
}

代码运行成功,没有抛出错误,但没有数据添加到我的 plist 文件中。

最佳答案

正如@logancautrell 所提到的,您不能在 mainbundle 中编写,您可以将 plist 保存在应用程序文档文件夹中,您可以这样做:

 NSString *path = @"example.plist";
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count]> 0)? [paths objectAtIndex: 0]: nil;
NSString *documentPath = [basePath stringByAppendingPathComponent:path] // Documents
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL checkfile = [fileManager fileExistsAtPath: documentPath];
NSLog(@"%@", (checkFile ? @"Exist": @"Not exist"));//check if exist
if(!checkfile) {//if not exist
BOOL copyFileToDoc = [yourDictionary writeToFile:documentPath atomically: YES];
NSLog(@"%@",(copyFileToDoc ? @"Copied": @"Not copied"));
}

关于xcode - 将数据添加到 plist 文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969432/

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