我是 iPhone 开发新手。我使用下面的代码将值添加到 plist,但是在执行代码后检查 plist 时,我没有看到 plist 中保存的任何值。我哪里做错了?我创建的 plist 位于资源文件夹中。
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"regvalue.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
[plistDict setValue:@"hello" forKey:@"choice"];
[plistDict writeToFile:finalPath atomically: YES];
用于检索值
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"regvalue.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
NSString *value;
value = [plistDict objectForKey:@"choice"];
NSLog(@"the value is %@",value);
它只给出空值。
最佳答案
资源文件夹不可写。您应该将其保存到文档文件夹中。
关于iphone - 如何在 iPhone 中保存 plist 文件中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2621515/