1
votes

I'm trying to store data of a custom type with core data. My class (friendItem) has one property (friend) of type NSData, to store it.

After archiving every elements of my array of type KNSelectorItem to NSData, I store them as "friend" in friendItem. But when I want to unarchive every elements of my array to read the information as KNSelectorItem objects, I get this error:

-[FriendItems bytes]: unrecognized selector sent to instance 0x10a637dc0

Code that might help:

HomeViewController.m

    -(void)selector:(KNMultiItemSelector *)selector didFinishSelectionWithItems:(NSArray  *)selectedItems
    {
      for (KNSelectorItem * i in selectedItems) {
      NSLog(@"ID: %@ NAME: %@", i.selectValue, i.displayValue);
      NSData *friendtosave = [NSKeyedArchiver archivedDataWithRootObject:i];
     [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        FriendItems *friend = [FriendItems MR_createEntityInContext:localContext];
        friend.friend = friendtosave;
     }];

    }

In ViewDidLoad of HomeViewController.m

    NSArray *friendsdata = [FriendItems MR_findAll];
    NSLog(@"friends saved as data %@", friendsdata);

    NSMutableArray *unarchievedFriends = [NSMutableArray array];

    for (NSData *i in friendsdata)
    {
        KNSelectorItem *friendDecrypted = [NSKeyedUnarchiver unarchiveObjectWithData:i];
        [unarchievedFriends addObject:friendDecrypted]; // THIS IS WHERE IT FAILS WHEN CHECKING WITH BREAKPOINTS
        NSLog(@"friend decrypted : %@", friendDecrypted.displayValue);

    }

What did I do wrong ? What did I miss ?

PS: I'm using Magical Record to manage my core data.

Thanks.

1

1 Answers

1
votes

Your unarchiving code does not make use of the friends relationship. You never look up the encoded data, you try to unarchive the FriendItems object directly. In the second code snippet:

  • friendsdata is an array of FriendItems
  • You iterate over that array, which means that i is actually a FriendItems instance.
  • You try to unarchive i, which is not an NSData, so you get the error you're seeing.

Based on what you've said, you need to change the loop to

for (FriendItems i in friendsdata) {

And then unarchive i.friendsdata instead of i.