为什么NSMutableDictionary不想写入文件? [英] Why NSMutableDictionary don't want write to file?
问题描述
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[NSFileManager defaultManager] fileExistsAtPath:pathString])
{
infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pathString];
}
else
{
infoDict = [[NSMutableDictionary alloc]initWithObjects:[NSArray arrayWithObjects:@"BeginFrame",@"EndFrame", nil] forKeys:[NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithBool:YES], nil]];
if ([infoDict writeToFile:pathString atomically:YES])
{
NSLog(@"Created");
}
else
{
NSLog(@"Is not created");
NSLog(@"Path %@",pathString);
}
}
这是我的代码。我检查文件是否已创建 - 如果没有 - 我创建 NSMutableDictionary
并将其写入路径中的文件,但是 writeToFile
方法返回否
。哪里有问题?如果我使用 NSFileManager
创建此文件,它可以工作,但是当我想写一个字典时,它不会。
This is my code. I check if file is created, if not - I create a NSMutableDictionary
and I write it to file at path, but writeToFile
method returns NO
. Where is problem? If I create this file with NSFileManager
it works, but doesn't when I want to write a dictionary.
推荐答案
writeToFile:atomically
仅在您调用它的字典是有效的属性列表对象时才有效(查看文档)。
writeToFile:atomically
only works if the dictionary you call it on is a valid property list object (see docs).
要使 NSDictionary
成为有效的属性列表对象,其中包括键必须是字符串,但在您的示例中,键是 NSNumber
实例。
For a NSDictionary
to be a valid property list object, among other things, its keys must be strings, but in your example the keys are NSNumber
instances.
这篇关于为什么NSMutableDictionary不想写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!