NSKeyedArchiver archiveRootObject不断返回NO [英] NSKeyedArchiver archiveRootObject keeps returning NO

查看:69
本文介绍了NSKeyedArchiver archiveRootObject不断返回NO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去的几个小时我一直在努力解决这个问题,但我的想法已经用光了.

I've spent the past few hours trying to figure this out, but I've ran out of ideas.

我要做的只是归档一个对象,但是方法 archiveRootObject 继续返回 NO

All I'm trying to do is archive an object, but the method archiveRootObject keeps on returning NO

这是我的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MyAppCache"];
NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:@"archive.data"];

if(![[NSFileManager defaultManager] fileExistsAtPath:fullPath]){
    [[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
}

NSArray *array = [NSArray arrayWithObjects:@"hello", @"world", nil];
NSLog(@"Full Path: %@", fullPath);
BOOL res = [NSKeyedArchiver archiveRootObject:array toFile:fullPath];

if(res){
    NSLog(@"YES");
}else{
    NSLog(@"NO");
}

每次我运行此命令时,它都会显示 NO .

Every time time I run this, it prints NO.

任何帮助将不胜感激!

推荐答案

您创建路径为 fullPath 的目录,然后尝试在同一路径下写入文件.用这样的文件覆盖目录是不可能的.使用您的 cacheDirectory 字符串创建目录.

You create a directory with the path fullPath and then you try to write the file at the same path. Overwriting a directory with a file like this is not possible. Use your cacheDirectory string to create your directory.

这篇关于NSKeyedArchiver archiveRootObject不断返回NO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆