1

I need to save array in core data, so i read that I can use NSData for it. So I think that I have problem with archiving.

NSMutableArray *newArray = [[NSMutableArray alloc]init];
NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:newArray];
[myEntity setValue:newData forKey:@"nameOfMyData"];

And then I try to pick my array in another VIewController for filling

NSData *newdata = [NSData dataWithData:self.myEntity.nameOfMyData];
NSMutableArray *photoArray = [[NSMutableArray alloc]init];
photoArray = [NSKeyedUnarchiver unarchiveObjectWithData:newdata];

I have no crash, but in command line appear next:

[NSKeyedUnarchiver initForReadingWithData:]: data is empty; 
did you forget to send -  finishEncoding to the NSKeyedArchiver?

And when i try to add object to my array, it does not add

[photoArray addObject:myImage];

So myImage is creating and with it I have no trouble, but in debugger always write for photoArray:

photoArray = (NSMutableArray*) 0x00000000 0 objects
4

1 回答 1

3

它应该工作。但是在取消归档数组时,请使用:

NSData *newdata = [NSData dataWithData:self.myEntity.nameOfMyData];
NSMutableArray *photoArray = [NSMutableArray arrayWithArray: [NSKeyedUnarchiver unarchiveObjectWithData:newdata]];

您的数组中有不符合 NSCoding 协议的对象,或者您没有保存核心数据上下文(或保存不成功)。

存档后

NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:newArray];

当您检查 newData 是否为零时,您会看到什么?我想它是零。

于 2012-12-17T19:11:41.770 回答