Storing NSManagedObject in a dictionary (NSDictionary)

12,270

Are you sure the error happens when you create the dictionary like it is implied by the code you have posted?

Property list invalid for format: 200 sounds like you try to write your NSManagedObject to the file system. Which won't work, because NSManagedObjects don't confirm to NSCoding.

You could save the attributes of the NSManagedObject in a NSDictionary, and save this dictionary to a file..

NSArray *keys = [[[myObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];

and when you want to restore it you set the values of your managedObject like this:

[myObject setValuesForKeysWithDictionary:dict];
Share:
12,270

Related videos on Youtube

antalkerekes
Author by

antalkerekes

iOS developer

Updated on March 19, 2020

Comments

  • antalkerekes
    antalkerekes about 4 years

    I have a custom class, which is a subclass of NSManagedObject. I would like to store it in a dictionary, but when trying to do so I receive a Property list invalid for format: 200 error.

    Here is how I try to create the dictionary:

    NSDictionary *dictionary = 
        [NSDictionary dictionaryWithObject: voiceMemo forKey:@"voiceMemo"];
    

    Same result when trying

    NSDictionary *dictionary = 
        [NSDictionary dictionaryWithObject: (NSData *) voiceMemo forKey:@"voiceMemo"];
    

    It works, however, when trying to save the individual attributes separately:

    NSDictionary *dictionary = 
        [NSDictionary dictionaryWithObject: voiceMemo.attribute forKey:@"attribute"];
    

    NSDictionary should be able to store data objects, so I'm guessing, the real question is how to cast an NSManagedObject object to NSData

  • antalkerekes
    antalkerekes about 13 years
    You are right, the error occurs at the next line, when I am attempting to write the data (in this case in a UILocalNotification's userData). As I wrote in the question, saving the attributes separately does work, so this solves the problem. I just thought (or hoped) that there was a way to get around this. Thank you!
  • Manobala
    Manobala about 12 years
    I searched Google for get array of keys, NSArray *keys = [[[myObject entity] attributesByName] allKeys]; is best than other answers thank you!
  • Ameet Dhas
    Ameet Dhas almost 11 years
    @Matthias how should i get the relationship objects in same dictionary?

Related