uitableview - Understanding a logic in update/add - NSUserDefaults (Objective C) -
i'm writing nice class handle call block in nsuserdefaults
. haven't tested yet, think miss regarding update/add callblockid
. code:
my code:
@implementation userdefaulthandler + (userdefaulthandler *)sharedinstance { static userdefaulthandler *instance; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ instance = [[userdefaulthandler alloc] init]; }); return instance; } -(void)updateoraddcallblock:(nsmutabledictionary*)callblockobject { nsstring *callblockid = [callblockobject objectforkey:@"callblockid"]; bool needtoupdate = no; nsinteger indexdict = 0; nsmutablearray *arrayofallcallblock = [[nsmutablearray alloc] init]; arrayofallcallblock = (nsmutablearray*)[[nsuserdefaults standarduserdefaults] objectforkey:@"arrayofallcallblock"]; nsmutabledictionary *newdict = [[nsmutabledictionary alloc] init]; if (arrayofallcallblock.count <= 0 || arrayofallcallblock == nil) { [arrayofallcallblock addobject:callblockobject]; } else { (int = 0; < arrayofallcallblock.count; i++) { nsmutabledictionary *mutabledict = [arrayofallcallblock objectatindex:i]; nsstring *idfromdict = [mutabledict objectforkey:@"callblockid"]; if ([idfromdict isequaltostring:callblockid]) { needtoupdate = yes; indexdict = i; newdict = mutabledict; } } if (needtoupdate) { [arrayofallcallblock removeobjectatindex:indexdict]; [arrayofallcallblock insertobject:newdict atindex:indexdict]; } else { [arrayofallcallblock addobject:callblockobject]; } } [self saveglobaldict:arrayofallcallblock]; } - (nsmutabledictionary *)getcallblockbyid:(nsstring*)callblockid { nsmutablearray *arrayofallcallblock = (nsmutablearray*)[[nsuserdefaults standarduserdefaults] objectforkey:@"arrayofallcallblock"]; bool isfound = no; nsinteger indexdict = 0; nsmutabledictionary *newdict = [[nsmutabledictionary alloc] init]; (int = 0; < arrayofallcallblock.count; i++) { nsmutabledictionary *mutabledict = [arrayofallcallblock objectatindex:i]; nsstring *idfromdict = [mutabledict objectforkey:@"callblockid"]; if ([idfromdict isequaltostring:callblockid]) { isfound = yes; indexdict = i; newdict = mutabledict; } } if (isfound) { return newdict; } else { return nil; } } - (nsmutablearray*)getallcallblock { nsmutablearray *arrayofallcallblock = (nsmutablearray*)[[nsuserdefaults standarduserdefaults] objectforkey:@"arrayofallcallblock"]; return arrayofallcallblock; } - (void)clearcallblockdb { [[nsuserdefaults standarduserdefaults] removeobjectforkey:@"arrayofallcallblock"]; [[nsuserdefaults standarduserdefaults] synchronize]; } - (void)saveglobaldict:(nsmutablearray*)globalarray { self.globalarray = [[nsmutablearray alloc] init]; self.globalarray = globalarray; [self clearcallblockdb]; [[nsuserdefaults standarduserdefaults] setobject:self.globalarray forkey:@"arrayofallcallblock"]; [[nsuserdefaults standarduserdefaults] synchronize]; } - (void)clearsinglecallblock:(nsstring*)callblockid { nsmutablearray *array = [[nsmutablearray alloc] init]; array = [self getallcallblock]; nsmutabledictionary *specificdict = [[nsmutabledictionary alloc] init]; nsinteger getindex = [self getindexforcallblock:array specificdict:specificdict]; [array removeobjectatindex:getindex]; [self saveglobaldict:array]; } - (nsinteger)getindexforcallblock:(nsmutablearray*)arrayofallcallblock specificdict:(nsmutabledictionary*)specificdict { nsinteger getindex = 0; (int = 0; < arrayofallcallblock.count; i++) { nsmutabledictionary *mutabledict = [arrayofallcallblock objectatindex:i]; nsdictionary *dict_one = [specificdict copy]; nsdictionary *dict_two = [mutabledict copy]; if ([dict_two isequaltodictionary:dict_one]) { getindex = i; } } return getindex; } @end
i'll explain: i've uitableview , when add row it, i've new window put details, save , present in uitableview. also, i've edit button can edit , update row in table. adding row table create unique string (callblockid). i'm not sure how handle , whether or not code enough.
any ideas?
Comments
Post a Comment