ios - Replace item in NSMutablearray -
i have nsmutablearray have data saved to. data input on main viewcontroller. when tap on saved item, secondviewcontroller has textfield data entry loaded in order edited. want able edit item , save array.
i new objective-c / cocoa touch please go easy on me.
- (todoitem *) updatetodoitem: (todoitem *) todoitem { nslog(@"todoitemsvc.updatetodoitem: %@", [todoitem description]); nsmutablearray *todoitems = [nsmutablearray array]; (nsstring *todoitem in todoitems) { if ([[todoitem] isequaltostring:todoitems]) { [todoitems replaceobjectatindex:todoitem]; } }
i know isnt correct , may off base im hoping moving in right direction this.
edit
here todoitemsvcarchive.m file
#import "todoitemsvcarchive.h" @implementation todoitemsvcarchive nsstring *filepath; nsmutablearray *todoitems; - (id) init { nsarray *dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *docsdir = [dirpaths objectatindex:0]; filepath = [[nsstring alloc] initwithstring: [docsdir stringbyappendingpathcomponent: @"todoitems.archive"]]; [self readarchive]; return self; } - (void) readarchive { nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: filepath]) { todoitems = [nskeyedunarchiver unarchiveobjectwithfile: filepath]; } else { todoitems = [nsmutablearray array]; } } - (void) writearchive { [nskeyedarchiver archiverootobject:todoitems tofile:filepath]; } - (todoitem *) createtodoitem: (todoitem *) todoitem { nslog(@"todoitemsvc.createtodoitem: %@", [todoitem description]); [todoitems addobject:todoitem]; [self writearchive]; return todoitem; } - (nsmutablearray *) retrievealltodoitems { return todoitems; } - (todoitem *) updatetodoitem: (todoitem *) todoitem { nslog(@"todoitemsvc.updatetodoitem: %@", [todoitem description]); nsmutablearray *todoitems = [nsmutablearray array]; (nsstring *todoitem in todoitems) { if ([[todoitem] isequaltostring:todoitems]) { [todoitems replaceobjectatindex:todoitem]; } } // return todoitem; } - (todoitem *) deletetodoitem: (todoitem *) todoitem { nslog(@"todoitemsvc.deletetodoitem: %@", [todoitem description]); [todoitems removeobject:todoitem]; [self writearchive]; return todoitem; } @end
edit
i guess dont understand how actual index of object needs replaced.
edit
i think better
- (todoitem *) updatetodoitem: (todoitem *) todoitem { nslog(@"todoitemsvc.updatetodoitem: %@", [todoitem description]); nsuinteger index = [todoitems indexofobject:todoitem]; [todoitems replaceobjectatindex:0 withobject:todoitem]; return todoitem; }
im still not sure how specify correct index.
over complicating entire thing
changed todoitemtext todoitemobject , made todoitem rather nsstring. when click update, updates object , when go list, view calls reloaddata , view updated new object
- (ibaction)updatetodoitem:(id)sender { nslog(@"updatingtodoitem: entering"); todoitemobject.todoitem = todoitem.text; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view. todoitem.text = todoitemobject.todoitem; }
works champ now.
Comments
Post a Comment