ios - Problems Saving NSMutableArray with custom objects -
i have class called currentuser
, holds nsmutablearray
called listoffriends
. listoffriends
holds objects class friends
.
both currentuser
, friend
classes have nscoding
protocols handled. friend
object has property called userimg
, uiimage
. there's 1 problem. when try load lisoffriends
, friends don't come uiimages
.
if save friend, , try load again, comes uiimage
no problem. if object inside listoffriends
, doesn't come uiimage
, other informations loaded correctly.
my question is: how can store , load nsmutablearray
custom objects? declaration of friendlist:
currentuser.h
@property (nonatomic,strong)nsmutablearray *friendslist;
currentuser.m
- (void) encodewithcoder:(nscoder *)encoder { [encoder encodeobject:username forkey:@"username"]; [encoder encodeobject:friendslist forkey:@"friendslist"]; //testing direct friend saving [encoder encodeobject:[friendslist objectatindex:0] forkey:@"testfriend"]; [encoder encodeobject:_userpsw forkey:@"userpassword"]; [encoder encodeobject:uiimagepngrepresentation(userimg) forkey:@"userimg"]; [encoder encodeobject:userid forkey:@"userid"]; [encoder encodeobject:groups forkey:@"groups"]; [encoder encodeobject:_email forkey:@"email"]; } - (id)initwithcoder:(nscoder *)decoder { currentuser *returnuser = [[currentuser alloc] init]; returnuser.username = [decoder decodeobjectforkey:@"username"]; returnuser.email = [decoder decodeobjectforkey:@"email"]; //testing direct friend loading friend *testfriend = [decoder decodeobjectforkey:@"testfriend"]; returnuser.userpsw = [decoder decodeobjectforkey:@"userpassword"]; returnuser.userimg = [uiimage imagewithdata:[decoder decodeobjectforkey:@"userimg"]]; returnuser.friendslist = [decoder decodeobjectforkey:@"friendslist"]; returnuser.userid = [decoder decodeobjectforkey:@"userid"]; returnuser.groups = [decoder decodeobjectforkey:@"groups"]; return returnuser; }
friend.m
- (void)encodewithcoder:(nscoder *)encoder{ [encoder encodeobject:uiimagepngrepresentation(userimg) forkey:@"friendimg"]; [encoder encodeobject:username forkey:@"friendun"]; [encoder encodeobject:userid forkey:@"friendid"]; } - (id)initwithcoder:(nscoder *)decoder{ friend *obj = [[friend alloc]init]; obj.username = [decoder decodeobjectforkey:@"friendun"]; obj.userimg = [uiimage imagewithdata:[decoder decodeobjectforkey:@"friendimg"]]; obj.userid = [decoder decodeobjectforkey:@"friendid"]; return obj; }
this object have save , load currentuser, it's called "configurations". configurations.h
#import "currentuser.h" @interface configurations : nsobject - (currentuser*)loadcustomobject; - (void)savecustomobject:(currentuser *)object; @end
configurations.m
#import "configurations.h" @implementation configurations - (void)savecustomobject:(currentuser *)object { nsdata *encodedobject = [nskeyedarchiver archiveddatawithrootobject:object]; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setobject:encodedobject forkey:@"lastuser"]; [defaults synchronize]; } - (currentuser*)loadcustomobject { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsdata *encodedobject = [defaults objectforkey:@"lastuser"]; currentuser *object = [nskeyedunarchiver unarchiveobjectwithdata:encodedobject]; return object; } @end
this how i'm loading currentuser.
saver = [[configurations alloc]init]; currentuser *tmpuser = [saver loadcustomobject]; thisuser = [currentuser instance]; static dispatch_once_t once; dispatch_once(&once, ^ { if(tmpuser != nil){ if(![tmpuser.username isequaltostring:@""]){ for(friend *savedfriend in tmpuser.friendslist) { [thisuser addfriend:savedfriend]; } thisuser.username = tmpuser.username; thisuser.email = tmpuser.email; thisuser.userid = tmpuser.userid; thisuser.userpsw = tmpuser.userpsw; thisuser.userimg = tmpuser.userimg; });
your initwithcoder:
methods incorrect. need in following pattern:
- (instancetype)initwithcoder:(nscoder *)decoder { self = [super init]; // or [super initwithcoder:decoder] if appropriate if (self) { _someivar = [decoder decodeobjectforkey:@"somekey"]; _someotherivar = [decoder decodeobjectforkey:@"someotherkey"]; } return self; }
fixing initwithcoder:
method both currentuser
, friend
classes lot.
currentuser:
- (id)initwithcoder:(nscoder *)decoder { self = [super init]; if (self) { _username = [decoder decodeobjectforkey:@"username"]; _email = [decoder decodeobjectforkey:@"email"]; //testing direct friend loading friend *testfriend = [decoder decodeobjectforkey:@"testfriend"]; _userpsw = [decoder decodeobjectforkey:@"userpassword"]; _userimg = [uiimage imagewithdata:[decoder decodeobjectforkey:@"userimg"]]; _friendslist = [decoder decodeobjectforkey:@"friendslist"]; _userid = [decoder decodeobjectforkey:@"userid"]; _groups = [decoder decodeobjectforkey:@"groups"]; } return self; }
friend:
- (id)initwithcoder:(nscoder *)decoder { self = [super init]; if (self) { _username = [decoder decodeobjectforkey:@"friendun"]; _userimg = [uiimage imagewithdata:[decoder decodeobjectforkey:@"friendimg"]]; _userid = [decoder decodeobjectforkey:@"friendid"]; } return self; }
Comments
Post a Comment