ios - Encode with coder not being called on lower level object -
i'm updating existing app follow mvc design. created top level data model class. in datamodel class archive , dearchive children array (self.children). array of child objects items property. items property array of babymilestone objects. both child class , babymilestone class take care of encode/decoding themselves. objects in child class being encoded , decoded. objects in babymilestone class not. encode coder not being called on babymilestone objects being called on child class.
question : need special encode array of objects property of else? debugging suggestions welcome!
the top level datamodel class handles saving , loading of .plist
@implementation datamodel -(id)init { if ((self = [super init])) { [self loadbabymilestones]; } return self; } /////////////loading , saving methods//////////////// //path documentsdirectory loading , saving .plist file of babymilestone objects -(nsstring *)documentsdirectory { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths firstobject]; return documentsdirectory; } -(nsstring *)datafilepath { return [[self documentsdirectory] stringbyappendingpathcomponent:@"babymilestones.plist"]; nslog(@"the documents directory %@", [self documentsdirectory]); } //save babymilestoneitems .plist -(void) savebabymilestoneitems { child *child; child = [[child alloc] init]; child = [self.children objectatindex:0 ]; nslog(@"the length of children array in save method %lu", child.items.count); nsmutabledata *data = [[nsmutabledata alloc] init]; nskeyedarchiver *archiver = [[nskeyedarchiver alloc] initforwritingwithmutabledata:data]; [archiver encodeobject:self.children forkey:@"children"]; [archiver finishencoding]; [data writetofile:[self datafilepath] atomically:yes]; nslog(@"in savebabymilestoneitems"); } //loading babymilestones data .plist file -(void) loadbabymilestones { nsstring *path = [self datafilepath]; nslog(@"data file path %@", [self datafilepath]); if ([[nsfilemanager defaultmanager]fileexistsatpath:path]) //if file exists load self.children { nsdata *data = [[nsdata alloc] initwithcontentsoffile:path]; nskeyedunarchiver *unarchiver = [[nskeyedunarchiver alloc] initforreadingwithdata:data]; self.children = [unarchiver decodeobjectforkey:@"children"]; [unarchiver finishdecoding]; nslog(@"in loadbabymilestones loading file"); } //default list else { self.children = [[nsmutablearray alloc]initwithcapacity:2]; child *child; child = [[child alloc] init]; child.name = @"test child 1"; [self.children addobject:child]; child = [[child alloc]init]; child.name = @"test child 2"; [self.children addobject:child]; (child *child in self.children) { babymilestone *babyitem; babyitem = [[babymilestone alloc] init]; babyitem.text = @"arrives home"; babyitem.backgroundimagename = @"newtext_home"; babyitem.imagename = @"flat_home_icon.png"; babyitem.textdescription = nil; babyitem.highresimage = @"newtext_homehighres.jpg"; babyitem.newitem = no; babyitem.date = nil; babyitem.nativeitem = yes; babyitem.photoid = @-1; [child.items addobject:babyitem]; } } } @end
the babymilestone class (encodewithcoder never called)
#import "babymilestone.h" @implementation babymilestone -(id)initwithcoder:(nscoder *)adecoder { if ((self=[super init])) { self.textdescription = [adecoder decodeobjectforkey:@"textdescription"]; self.text = [adecoder decodeobjectforkey:@"text"]; self.photoid = [adecoder decodeobjectforkey:@"photoid1"]; self.imagename = [adecoder decodeobjectforkey:@"thumbnail"]; self.date = [adecoder decodeobjectforkey:@"date"]; self.backgroundimagename = [adecoder decodeobjectforkey:@"backgroundimage"]; self.highresimage = [adecoder decodeobjectforkey:@"highresimage"]; self.nativeitem = [adecoder decodeboolforkey:@"nativeitem"]; } return self; } -(void) encodewithcoder:(nscoder *)acoder { [acoder encodeobject:self.textdescription forkey:@"textdescription"]; [acoder encodeobject:self.text forkey:@"text"]; [acoder encodeobject:self.photoid forkey:@"photoid1"]; [acoder encodeobject:self.imagename forkey:@"thumbnail"]; [acoder encodeobject:self.date forkey:@"date"]; [acoder encodeobject:self.backgroundimagename forkey:@"backgroundimage"]; [acoder encodeobject:self.highresimage forkey: @"highresimage"]; [acoder encodebool:self.nativeitem forkey:@"nativeitem"]; nslog(@"in encode coder in babymilestone.m"); } @end
child class
#import "child.h" @implementation child - (id)init { //used when user add new child if ((self = [super init])) { self.items = [[nsmutablearray alloc] initwithcapacity:20]; } return self; } -(id)initwithcoder:(nscoder *)adecoder //load existing children { if ((self=[super init])) { self.name = [adecoder decodeobjectforkey:@"name"]; //self.birthday = [adecoder decodeobjectforkey:@"birthday"]; nslog(@"init coder being called on child class"); } return self; } -(void)encodewithcoder:(nscoder *)acoder { [acoder encodeobject:self.name forkey:@"name"]; nslog(@"encodewithcoder called in child class"); //[acoder encodeobject:self.birthday forkey: @"birthday]; } @end
your child
objects, keepers of babymilestones
, ones responsible encoding them. add [acoder encodeobject:self.items forkey:@"items"];
child
's encodewithcoder:
. way, when child
told encode, encode babymilestones
well.
Comments
Post a Comment