Neznajka
Neznajka

Reputation: 305

NSArray to NSData for Core Data

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

Upvotes: 1

Views: 4769

Answers (1)

Mario
Mario

Reputation: 4520

It should work. But when unarchiving the array rather use:

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

You either have objects in your array that dont conform to NSCoding protocol or you do not save your core data context (or saving is unsuccessful).

After archiving with

NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:newArray];

what do you see when you check newData for nil? I suppose it is nil.

Upvotes: 3

Related Questions