错误消息说我的数据为 NULL - NSKeyedUnArchiver - 我已按照所有步骤操作 - 仍然无法工作

Posted

技术标签:

【中文标题】错误消息说我的数据为 NULL - NSKeyedUnArchiver - 我已按照所有步骤操作 - 仍然无法工作【英文标题】:Error message is saying my data is NULL - NSKeyedUnArchiver - I've followed all the steps - still won't work 【发布时间】:2014-12-30 00:41:58 【问题描述】:

我想将 NSMutableArray *allRedItems 保存到文件中。它包含自定义对象,Itemz:

@interface Itemz : NSObject <NSCoding>
@property double price;
@property double quality;
@property double style;


@property double overallAverage;

到目前为止,我已经完成了所有正确的步骤:

@implementation Itemz

- (void)encodeWithCoder:(NSCoder *)encoder 


[encoder encodeDouble:self.price forKey:@"price"];
[encoder encodeDouble:self.style forKey:@"style"];
[encoder encodeDouble:self.quality forKey:@"quality"];
[encoder encodeDouble:self.overallAverage forKey:@"overallAverage"];

[encoder encodeBool: self.isClicked forKey:@"isClicked"];


- (id)initWithCoder:(NSCoder *)decoder 
if((self = [super init])) 
    //decode properties, other class vars
    self.price = [decoder decodeDoubleForKey:@"price"];

    self.style = [decoder decodeDoubleForKey:@"style"];
    self.quality = [decoder decodeDoubleForKey:@"quality"];
    self.overallAverage = [decoder decodeDoubleForKey:@"overallAverage"];
    self.isClicked = [decoder decodeBoolForKey:@"isClicked"];



return self;

@end

这是我保存数组 *allRedItems 的地方

@implementation AllRedItems
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.allRedItems];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *allItemsRed = [documentsDirectory  stringByAppendingPathComponent:@"allItemsRed"];

[data writeToFile:allItemsRed atomically:YES];
 @end  

当我检索它时,它给了我这个错误 - [NSKeyedUnarchiver initForReadingWithData:]: data is NULL

@implementation ItemSuggestion
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:@"allItemsRed"];
NSData * data = [NSData dataWithContentsOfFile:path];
NSMutableArray *allRedItems = [NSKeyedUnarchiver unarchiveObjectWithData:data];
@end

我错过了什么吗?还是我需要添加一些东西?

【问题讨论】:

除其他外,您忽略了编译和执行之间的区别。 对不起,我是新手,我收到的错误消息是 [NSKeyedUnarchiver initForReadingWithData:]: data is NULL 。我缺少的“其他东西”是什么?我对你的说法感到困惑 【参考方案1】:

你把它保存在NSDocumentationDirectory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

但是从NSDocumentDirectory找回,所以你拿不到。

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

在这两种方法中使用NSDocumentDirectory

【讨论】:

以上是关于错误消息说我的数据为 NULL - NSKeyedUnArchiver - 我已按照所有步骤操作 - 仍然无法工作的主要内容,如果未能解决你的问题,请参考以下文章

IIS发布网站总是说我配置文件报错

Firebase 电子邮件说我的实时数据库有不安全的规则

iTunes Connect 说我的 Icon.png 文件是 0x0 .. 显然不是!

解决错误Android Studio:安装失败,消息为null

当我的代码工作时出现“Rich Embed fields may not be empty”错误

在 access 数据库中将 Null 转换为零