NSKeyedUnarchiver unarchiveObjectWithFile:在init:方法中返回nil [英] NSKeyedUnarchiver unarchiveObjectWithFile: returns nil in init: method

查看:593
本文介绍了NSKeyedUnarchiver unarchiveObjectWithFile:在init:方法中返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这个工作,如果我调用archiveRootObject:从applicationDidFinishLaunching:。如果我从一个singleton类的init:方法调用它,它返回nil。

Update: This works if I call archiveRootObject: from applicationDidFinishLaunching:. If I call it from the init: method of a singleton class, it returns nil.

我非常困惑的NSKeyedUnarchiver unarchiveObjectWithFile:的行为。 文档说如果文件不存在,它将返回nil。使用我的对象之一,会发生以下情况:

I'm very confused by the behavior of NSKeyedUnarchiver unarchiveObjectWithFile:. The documentation says that it will return nil if the file doesn't exist. With one of my objects, the following happens:

Game *g1 = [Game getGame];
NSString *archivePath = [Game getArchivePath];
bool success = [NSKeyedArchiver archiveRootObject:g1 toFile:archivePath];
Game *g2 = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];

// success is true, g2 is nil

文件实际上确实存在并且正在被archiveRootObject:方法写入。

I've verified that the file actually does exist and is getting written to by the archiveRootObject: method. What am I doing wrong preventing me from getting a Game object back out of the archive?

推荐答案

我遇到了同样的麻烦在Xcode 4.1中与ARC支持:

I've meet the same trouble in Xcode 4.1 with ARC support:

BOOL isFileExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
NSAssert(isFileExist, @"filePath does not exist");
NSKeyedUnarchiver* coder =
    [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; // nil
NSData* data = [[NSFileManager defaultManager] contentsAtPath:filePath];
coder = [NSKeyedUnarchiver unarchiveObjectWithData:data]; // nil
coder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // OK!!!

这似乎是可可触摸的错误。

It seems to be a bug in cocoa-touch.

这篇关于NSKeyedUnarchiver unarchiveObjectWithFile:在init:方法中返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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