writeToFile:atomically: 不保存新的 plist 数据
Posted
技术标签:
【中文标题】writeToFile:atomically: 不保存新的 plist 数据【英文标题】:writeToFile:atomically: not saving new plist data 【发布时间】:2013-05-01 18:45:21 【问题描述】:每次启动应用程序时,我都使用writeToFile:atomically:
将 plist 中的键值更新 1。我将这段代码放在viewDidLoad
中,它读取键的字符串值,获取该字符串的数值,将其增加 1,将其转换回字符串,并将其写入该键的新字符串,但是当我再次阅读它时,它似乎没有更新。我无法弄清楚我做错了什么。我不需要writeToFile:atomically:
的特殊框架,对吗?
代码如下:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DaysLaunched.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"DaysLaunched" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:path error:&error];
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *currentNumberOfDays = [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"numberOfDays"]];
NSLog(@"currentNumberOfDays = %@", currentNumberOfDays); //0
int days = [currentNumberOfDays intValue];
days ++;
currentNumberOfDays = [NSString stringWithFormat:@"%d", days];
[data1 setObject:[NSString stringWithFormat:@"numberOfDays"] forKey:currentNumberOfDays];
NSLog(@"currentNumberOfDays = %@", currentNumberOfDays); //1
[data1 writeToFile: path atomically:YES];
currentNumberOfDays = [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"numberOfDays"]];
NSLog(@"currentNumberOfDays = %@", currentNumberOfDays); //0 ??????? writeToFile isn't working?
这是我的“支持文件”文件夹中“DaysLaunched.plist”的屏幕截图(我还通过复制粘贴验证了 plist 文件名的拼写方式与我在代码中的拼写方式完全相同)
原始 plist 文件也在我的目标中的“复制捆绑资源”中。
【问题讨论】:
writeToFile:atomically:
是否返回 YES
? copyItemAtPath:toPath:error:
调用是否返回 YES
?如果不是,错误说明了什么?
@JoshCaswell,它成功了。没有返回错误。我不确定如何检查 writeToFile 是否返回“YES”,您能指导我这样做吗?
旁注 - 为什么numberOfDays
是字符串而不是数字?为什么要进行所有转换?如果是数字,则使用数字。并且请摆脱代码中对stringWithFormat:
的所有不必要的调用。大多数都不需要。
我想你已经在[data1 setObject:[NSString stringWithFormat:@"numberOfDays"] forKey:currentNumberOfDays];
交换了参数,试试[data1 setObject:currentNumberOfDays forKey:@"numberOfDays"];
BTW - 当你写出新的 plist 时,它被写入 Documents 目录(这是正确的)。所以查看包中的 plist 文件是没有意义的。
【参考方案1】:
试试
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DaysLaunched.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"DaysLaunched"
ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:path error:&error];
NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSInteger days = [plistDict[@"numberOfDays"] integerValue];
plistDict[@"numberOfDays"] = [@(++days) stringValue];
[plistDict writeToFile: path atomically:YES];
【讨论】:
【参考方案2】:您在设置数据时犯了一个错误。
你应该这样做:
[data1 setObject:currentNumberOfDays forKey:@"numberOfDays"];
代替:
[data1 setObject:[NSString stringWithFormat:@"numberOfDays"] forKey:currentNumberOfDays];
【讨论】:
以上是关于writeToFile:atomically: 不保存新的 plist 数据的主要内容,如果未能解决你的问题,请参考以下文章
writeToFile:atomically: 会阻止异步读取吗?