【问题标题】:NSCoding NSKeyedUnarchiver unarchiveObjectWithFile: returning nullNSCoding NSKeyedUnarchiver unarchiveObjectWithFile:返回 null
【发布时间】:2012-01-16 22:21:53
【问题描述】:

我正在尝试使用 NSCoding 从我的应用程序中保存一些值。我可以保存该值,但无法检索它。

这里是我声明协议的地方:

@interface AddReminderEventViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, NSCoding> 

这是我遵守协议的地方:

-(void)encodeWithCoder:(NSCoder *)enCoder
{
[enCoder encodeObject:self.eventRepeatDurationDate   forKey:kEventRepeatDuration];
[enCoder encodeObject:self.eventIDsMutableArray      forKey:kEventIDsMutableArray];
[enCoder encodeObject:self.eventRepeatDurationString forKey:@"mytest"];}

这里:

-(id)initWithCoder:(NSCoder *)decoder {

if (self = [super init]){

    self.eventRepeatDurationDate = [[decoder decodeObjectForKey:kEventRepeatDuration]  retain];
    self.eventIDsMutableArray    = [[decoder decodeObjectForKey:kEventIDsMutableArray] retain];
    self.eventRepeatDurationString = [[decoder decodeObjectForKey:@"mytest"]  retain];} return self; }

这里是我调用归档和取消归档方法的地方:

    [self saveDataToDisk];
    [self loadDataFromDisk];

这里是这些方法的主体和它的 NSLog 内容:

- (void)saveDataToDisk {
NSString *reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";    
//reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";
reminderEventIDsPathString = [reminderEventIDsPathString stringByExpandingTildeInPath];
NSLog(@"WATCH1: reminderEventIDsPathString is %@", reminderEventIDsPathString);

NSMutableDictionary *rootObject;
rootObject = [NSMutableDictionary dictionary];

[rootObject setValue:eventRepeatDurationString forKey:@"mytest"];
NSLog(@"1rootObject IS %@", rootObject);

[NSKeyedArchiver archiveRootObject:rootObject toFile:reminderEventIDsPathString];}

reminderEventIDsPathString is /Users/tester/Library/Application Support/iPhone Simulator/5.0/Applications/E26D57DE-C4E1-4318-AEDD-7207F41010A9/Library/Application Support/ReminderIDs.archive
2012-01-16 15:47:48.578 [29658:15503] 1rootObject IS {mytest = 7;}

这里是解压代码及其 NSLog 内容:

- (void)loadDataFromDisk {
NSString *testValue = [[NSString alloc] init];
NSString *reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";    
reminderEventIDsPathString = [reminderEventIDsPathString stringByExpandingTildeInPath];
NSLog(@"WATCH2: reminderEventIDsPathString is %@", reminderEventIDsPathString);

NSMutableDictionary *rootObject;
rootObject = [[NSKeyedUnarchiver unarchiveObjectWithFile:reminderEventIDsPathString] retain];

NSLog(@"2rootObject IS %@", rootObject);

NSLog(@"WATCH3 - %@", [rootObject objectForKey:@"mytest" ]);

if ([rootObject valueForKey:@"mytest"]) {
    testValue = [rootObject valueForKey:@"mytest"];
    NSLog(@"WATCH: testValue is %@", testValue); } }

2012-01-16 15:48:14.965 [29658:15503] WATCH2: reminderEventIDsPathString is /Users/tester/Library/Application Support/iPhone Simulator/5.0/Applications/E26D57DE-C4E1-4318-AEDD-7207F41010A9/Library/Application Support/ReminderIDs.archive

2012-01-16 15:48:17.879 [29658:15503] 2rootObject IS (null)

我无法取消归档内容,我错过了什么?我只是专注于我的编码器/解码器方法中最简单的值,只是为了测试它,但我什至无法让字符串值工作。

谢谢

【问题讨论】:

    标签: ios nskeyedarchiver nscoding


    【解决方案1】:

    您保存和加载提醒的路径错误。也许换成这个

    NSString *reminderEventIDsPathString = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ReminderIDs.archive"];
    

    【讨论】:

    • 你能告诉我你所说的“错误”是什么意思吗?我以为我应该写到图书馆路径。这是不正确的吗?另外,我在该路径上看到了该文件,并且其中包含内容。谢谢
    • 嗯,成功了!非常感谢你,但我也很感激你澄清为什么会这样。应用程序不应该写入/读取我的路径吗?感谢您花时间回答。
    • 另外,如果我想在一个视图控制器中写出值并在另一个视图控制器中检索它,我是否应该使用/引用 appDelegate 来进行写入和读取以避免重复代码?再次感谢。
    • 应用程序是沙箱的,它们只应该在自己的沙箱中写入和读取,可以使用 NSHomeDirectory() 轻松访问,您之前的路径是在应用程序的沙箱之外。如果要写入“Library”,只需将“Documents”替换为“Library”即可。
    • 如果你去 ~/Library/Application Support/iPhone Simulator/5.0/Applications/ 你可以看到随机名称的文件夹,这些文件夹里面是 Documents、Library、tmp。每个应用都有自己的空间,并且只能在那里读写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多