ios - Custom NSObject iniWithCoder not called -
i have custom object, levelcontent
, contains properties. levelcontent
conforms nscoding
, , have implemented encodewithcoder:
, initwithcoder:
methods. save , fetch data parse.com. saving works fine this:
nsdata *data = [nskeyedarchiver archiveddatawithrootobject:self.currentlevelcontent];
when fetch data, nsdata
correctly, when try initialize levelcontent
with downloaded data, initwithcoder:
never gets called. try load levelcontent
this:
levelcontent *content = [nskeyedunarchiver unarchiveobjectwithdata:data];
here code encoding/decoding
- (void)encodewithcoder:(nscoder *)acoder { [acoder encodeobject:self.tiles forkey:@"tiles"]; [acoder encodeobject:self.attributes forkey:@"attributes"]; [acoder encodeobject:self.level forkey:@"level"]; } - (instancetype)initwithcoder:(nscoder *)adecoder { if (self == [super init]) { self.tiles = [nsmutablearray array]; [self.tiles addobjectsfromarray:[adecoder decodeobjectforkey:@"tiles"]]; self.attributes = [nsmutabledictionary dictionary]; [self.attributes addentriesfromdictionary:[adecoder decodeobjectforkey:@"attributes"]]; self.level = [level levelwithtopindex:0 detailindex:0]; self.level = [adecoder decodeobjectforkey:@"level"]; } return self; }
change if (self == [super init])
if (self = [super init])
in initwithcoder:
try this,
- (instancetype)initwithcoder:(nscoder *)adecoder { if (self = [super init]) { ..... } return self; }
Comments
Post a Comment