Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 为什么NSMutableDictionary不';我不想写入文件?_Iphone_Ios_Nsmutabledictionary_Nsfilemanager - Fatal编程技术网

Iphone 为什么NSMutableDictionary不';我不想写入文件?

Iphone 为什么NSMutableDictionary不';我不想写入文件?,iphone,ios,nsmutabledictionary,nsfilemanager,Iphone,Ios,Nsmutabledictionary,Nsfilemanager,这是我的密码。我检查文件是否已创建,如果未创建-我创建一个NSMutableDictionary并将其写入路径处的文件,但writeToFile方法返回NO。问题在哪里?如果我使用NSFileManager创建此文件,它会工作,但在我想编写字典时不会工作。writeToFile:atomically仅当调用它的字典是有效的属性列表对象()时才工作 要使NSDictionary成为有效的属性列表对象,除其他外,还有its,但在您的示例中,键是NSNumber实例。有时您无法控制要编写的内容。例如,

这是我的密码。我检查文件是否已创建,如果未创建-我创建一个
NSMutableDictionary
并将其写入路径处的文件,但
writeToFile
方法返回
NO
。问题在哪里?如果我使用
NSFileManager
创建此文件,它会工作,但在我想编写字典时不会工作。

writeToFile:atomically
仅当调用它的字典是有效的属性列表对象()时才工作


要使
NSDictionary
成为有效的属性列表对象,除其他外,还有its,但在您的示例中,键是
NSNumber
实例。

有时您无法控制要编写的内容。例如,在编写从服务器获取的JSON对象时,无法避免使用
null

NSData
与这些“无效”值兼容,因此在这些情况下,将
NSArray
NSDictionary
转换为
NSData
是一种理想的方法

写:

- (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);
        }
}
阅读:


pathString
从何而来?我用[[NSFileManager defaultManager]createFileAtPath:pathString内容:nil属性:nil]说;这个文件是创建的…我有第一个metode init,在那里我分配了它。看起来你在初始化
infoDict
时犯了一个错误。我的意思是对象应该是键和键-对象。thx。。。这是个问题。。。但它在2011年运作良好。。。没有问题:谢谢!我只是被所有的键都必须是字符串的要求绊倒了。太棒了。如上所述,您可能并不总是能够序列化JSON。这对我的JSON非常有效。
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:jsonObject];
[data writeToFile:path atomically:YES];
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *jsonObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];