Note that there are some explanatory texts on larger screens.

plurals
  1. PONSCoding of NSMutableDictionaries containing custom objects
    text
    copied!<p>I was trying to serialize a <code>SearchEntity</code> object(custom object) containing an <code>NSMutableDictionary</code> containing a set of type <code>CategoryEntity</code>(custom object).</p> <p>1 <code>SearchEntity&lt;NSCoding&gt;</code> containing: 1 <code>NSMutableDictionary</code> (parameters) parameters containing X <code>CategoryEntities&lt;NSCoding&gt;</code> containing just strings and numbers.</p> <p>At this line <code>[encoder encodeObject:parameters forKey:kPreviousSearchEntityKey];</code> in the SearchEntity encodeWithCoder" I get GDB:Interrupted every time, no error message, exception etc. just GDB:Interrupted.</p> <p>This is the implementation in <code>SearchEntity</code> and parameters is the <code>NSMutableDictionary</code></p> <pre><code>#pragma mark - #pragma mark NSCoding delegate methods - (void) encodeWithCoder:(NSCoder*)encoder { //encode all the values so they can be persisted in NSUserdefaults if (parameters) [encoder encodeObject:parameters forKey:kPreviousSearchEntityKey]; //GDB:Interrupted! } - (id) initWithCoder:(NSCoder*)decoder { if (self = [super init]) { //decode all values to return an object from NSUserdefaults in the same state as when saved [self setParameters:[decoder decodeObjectForKey:kPreviousSearchEntityKey]]; } return self; } </code></pre> <p>The <code>CategoryEntity</code> also implements the NSCoding protocol and looks like this:</p> <pre><code>- (void) encodeWithCoder:(NSCoder*)encoder { //encode all the values so they can be persisted in NSUserdefaults [encoder encodeObject:ID forKey:kIDKey]; [encoder encodeObject:text forKey:kTextKey]; [encoder encodeObject:category forKey:kCategoryKey]; [encoder encodeObject:categoryIdentifierKey forKey:kCategoryIdentifierKey]; } - (id) initWithCoder:(NSCoder*)decoder { if (self = [super init]) { //decode all values to return an object from NSUserdefaults in the same state as when saved [self setID:[decoder decodeObjectForKey:kIDKey]]; [self setText:[decoder decodeObjectForKey:kTextKey]]; [self setCategory:[decoder decodeObjectForKey:kCategoryKey]]; [self setCategoryIdentifierKey:[decoder decodeObjectForKey:kCategoryIdentifierKey]]; } return self; } </code></pre> <p>I try to encode it from a wrapper for NSUserDefaults, like this:</p> <pre><code>+ (void) setPreviousSearchParameters:(SearchParameterEntity*) entity { if (entity) { //first encode the entity (implements the NSCoding protocol) then save it NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:entity]; [[self defaults] setObject:encodedObject forKey:kPreviousSearchKey]; [[self defaults] synchronize]; } } + (SearchParameterEntity*) getPreviousSearchParameters { //retrieve the encoded NSData object that was saved, decode and return it SearchParameterEntity *entity = nil; NSData *encodedObject = [[self defaults] objectForKey:kPreviousSearchKey]; if (encodedObject) entity = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject]; return entity; } </code></pre> <p>I was thinking that when I ask to Serialize the SearchEntity, it would start to serialize the 'parameters' mutableDictionary object, NSCoder will call "encode" on the CategoryEntities contained in the dictionary and they will all respond with their correct encoded objects.</p> <p>However I just get GDB:Interrupted in the bottom of the console.</p> <p><strong>How can I debug this?</strong></p> <p><strong>And is my approach wrong, should I wrap all levels of encoding in NSData?</strong></p> <p>Ps. I do the exact same thing with a <code>ResultEntity</code> containing NSArrays of <code>CategoryEntities</code>, it encodes with no problems, so I guess the NSMutableDictionary is the only thing sticking out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload