NSKeyedUnarchiver : cannot decode object of class

Hi there!

I'm being unable to decode through "NSKeyedUnarchiver.UnarchiveFile(documents)" an iOS saved plist file. It gives me the following error:

- Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidUnarchiveOperationException Reason: *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (FavoriteRequest).

I don't have that class declared, because i guess i don't need to. Am i right?

Can anyone help me please?

Thanks.

Tagged:

Best Answers

  • MichaelBluesteinMichaelBluestein US ✭✭✭
    Accepted Answer

    For NSKeyedArchiver to be able to unarchive, it needs the class FavoriteRequest. FavoriteRequest has to conform to the NSCoding protocol. On the C# side, you can expose the Objective-C class to a managed counterpart through the RegisterAttribute and use the ExportAttribute to bind the Objective-C initializer that takes an NSCoder to a constructor on the C# side.

Answers

  • MichaelBluesteinMichaelBluestein USInsider, University, Developer Group Leader ✭✭✭
    Accepted Answer

    For NSKeyedArchiver to be able to unarchive, it needs the class FavoriteRequest. FavoriteRequest has to conform to the NSCoding protocol. On the C# side, you can expose the Objective-C class to a managed counterpart through the RegisterAttribute and use the ExportAttribute to bind the Objective-C initializer that takes an NSCoder to a constructor on the C# side.

  • Thanks for your answer, although I did not fully understood what you told me to do to solve my problem. Can you be more specific? Can you provide me some code lines that could help me to understand what you're trying to explain me? Sorry, but i really didn't get it. I'm a newbie at iOS surroundings.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Where does FavoriteRequest come from? Is this your type? How did it get into the .plist file?

  • FavouriteRequest is the objects Type. The code i use to save the objects is the following:

    FavoriteRequest * favoriteRequest = [favoritesData objectAtIndex:i];

        if ([favoriteRequest.Tracks isEqualToArray:fr.Tracks])
        {
            UIImage * buttonImage = [UIImage imageNamed:@"favorited_white.png"];
            [favoriteButton setImage:buttonImage forState:UIControlStateNormal];
    
            FavoriteRequest * fr = [[FavoriteRequest alloc] initWithClass:request];
            [fr setCustomName:favoriteRequest.customName];
            NSMutableArray * newFavoritesData = [[NSMutableArray alloc] init];
            [newFavoritesData addObject:fr];
    
            int i=0;
    
            while (i<favoritesData.count)
            {
                FavoriteRequest * favoriteRequest = [favoritesData objectAtIndex:i];
                if (![favoriteRequest.Tracks isEqualToArray:fr.Tracks])
                {
                    [newFavoritesData addObject:favoriteRequest];
                }
                i++;
            }
    
            NSError  *error;
            NSData* archiveData = [NSKeyedArchiver archivedDataWithRootObject:newFavoritesData];
            [archiveData writeToFile:[OptGeneric dataFilePath:FAVORITE_ROUTING_FILENAME] options:NSDataWritingAtomic error:&error];
            return true;
        }
    
  • I already understood what you tried to explain, MichaelBluestein. Thank you very very much for your answer.

  • It was something like that, yes. Thank you for your post. I'm still new to the forum world. Next time i will post the solution.

    Thank you all!

Sign In or Register to comment.