Key Archiving a NSMutableDictionary object corrupts its embedded NSMutableDictionary data object
Key Archiving a NSMutableDictionary object corrupts its embedded NSMutableDictionary data object
- Subject: Key Archiving a NSMutableDictionary object corrupts its embedded NSMutableDictionary data object
- From: "Frederick C. Lee" <email@hidden>
- Date: Sun, 4 Jul 2004 13:53:40 -0700
Greetings:
The scenario:
I'm trying to archive a NSMutableDictionary of country objects, the
key being the country name. Each country object (model) contains
assorted country-related instance variables, including an embedded
NSMutableDictionary of city objects that are roughly patterned the same
way as their parent country.
{Country Dictionary}
['country name' --> (country object)]
{City Dictionary}
['city name' -->(city
object)]
...
...
I'm using key-archiving to store the country dictionary.
Problem: The embedded city dictionary always returns corrupt. Or
perhaps is written corrupted before the read. However, I'm able to
read the keys of the apparent corrupted NSMutableDictionary.
Here's the code:
From the Controller:
if (![NSKeyedArchiver archiveRootObject:theCountryMDict
toFile:countryDataSource]) {
NSLog(@"{saveDataToDisk} Sorry, the archiving failed.");
} else {
[[self undoManager] removeAllActions];
}
------------------------------------------------------------------------
----------------
The Country Model:
The country model contains the following {abridged} coder routines:
- (void)encodeWithCoder:(NSCoder *)coder {
...
[coder encodeObject:[self cities] forKey:kCities];
//...NSMutableDictionary
...
}
- (id)initWithCoder:(NSCoder *)decoder {
...
[self setCities:[decoder decodeObjectForKey:kCities]];
// ...NSMutableDictionary
...
return self;
}
------------------------------------------------------------------------
----------------
The accessors:
- (NSMutableDictionary *) cities {
return [[cities autorelease] retain];
}
- (void) setCities: (NSMutableDictionary *) theCities {
[theCities retain];
[cities release];
cities = theCities;
}
==========================================================
Reading the data in debugger:
(gdb) po [theCountryMDict objectForKey:@"France"]
Current Contents of CountryModel:
------
countryName:France
countryCode:33
currencyName:
cities:(Marseille, Paris) <--- Note: I can get the city keys from the
embedded "cities" NSMutableDictionary
------
(gdb) po [[theCountryMDict objectForKey:@"France"] cities] <--
However, I can't decipher the "cities" NSMutableDictionary
Program received signal EXC_BAD_ACCESS, Could not access memory.
0x908311e8 in objc_msgSend ()
The program being debugged was signaled while in a function called from
GDB.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off"
Evaluation of the expression containing the function
(_NSPrintForDebugger) will be abandoned.
=======================================================================
(gdb) po [[[theCountryMDict objectForKey:@"France"] cities] allKeys]
<--- Notice that I can get the KEYS. Okay.
<NSCFArray 0x552b80>(
Marseille,
Paris
)
(gdb) po [[[theCountryMDict objectForKey:@"France"] cities] allValues]
<-- But the values (CityModel.m) appear to be corrupt.
Program received signal EXC_BAD_ACCESS, Could not access memory.
0x908311e8 in objc_msgSend ()
The program being debugged was signaled while in a function called from
GDB.
GDB has restored the context to what it was before the call.
To change this behavior use "set unwindonsignal off"
Evaluation of the expression containing the function
(_NSPrintForDebugger) will be abandoned.
(gdb)
========================================================================
=
I've able to read/write city-level arrays & scalers within their
country objects of the country NSMutableDictionary. But I can't get a
clean access to the VALUES (City Model objects) from the embedded
"cities" mutable dictionary.
I'm using the standard key-archiving & de-archiving methods.
Does anyone have a clue to solve this mystery?
Thanks!
Regards,
Ric.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.