iphone - Reading and Writing to an NSDictionary? -
in app have table view user presses "add" button in nav bar , new cell added table view. data table loaded nsarray , each index in array storing nsmutabledictionary 4 key-value pairs. array saved .plist every time cell added. when user selects row in table detail view gets loaded. detail view loads data saved .plist depending on row selected.
in detail view want allow user edit data in dictionary specific row. i've been trying different things , can read data dictionary , load view when try , save data dictionary app keeps terminating.
can explain me proper way reading , writing data nsmutabledictionary?
this how i'm writing data .plist:
nsmutablearray *array = [[nsmutablearray alloc] initwithcontentsoffile:filepath]; nsmutabledictionary *dict = [[nsmutabledictionary alloc] init]; dict = [array objectatindex:selectedrow]; nsstring *tempa = [[nsstring alloc] initwithformat:@"%d pts", ivar_a]; nsstring *tempb = [[nsstring alloc] initwithformat:@"%d pts", ivar_b]; nsstring *tempc = [[nsstring alloc] initwithformat:@"%d pts", ivar_c]; [dict setvalue:tempa forkey:@"keya"]; [dict setvalue:tempb forkey:@"keyb"]; [dict setvalue:tempc forkey:@"keyc"]; [tempa release]; [tempb release]; [tempc release]; [array insertobject:dict atindex:selectedrow]; //save modified array plist [array writetofile:[self datafilepath] atomically:yes]; [dict release]; [array release];
if use [[nsmutabledictionary alloc] dictionaryfromcontentsoffile:@"pathtofile.plist], contents of dictionary still immutable, if top level container mutable. stated in apple's reference.
the dictionary representation in file identified path must contain property list objects (nsstring, nsdata, nsdate, nsnumber, nsarray, or nsdictionary objects). more details, see property list programming guide. objects contained dictionary immutable, if dictionary mutable.
if need make data structure writable again, need make copy of loaded data structure using mutable types, like
[nsmutabledictionary dictionarywithdictionary:oorig]
etc.
Comments
Post a Comment