xcode - Importing whole NSArray to core data -
xcode - Importing whole NSArray to core data -
i have uitextview, user come in copied text, structured in lines:
peter paul sonia edgar
then run script store text in lines in nsarray. finally, want import array core data, imports lastly object - edgar - why?
i have 2 functions - 1 fill array , sec loop through array , save context:
- (ibaction)savearray:(id)sender { if ([inputtv.text length] > 0) { // --------- decrypt text nsstring *importtxt = inputtv.text; nsmutablearray *importedtext = [[nsmutablearray alloc] init]; nscharacterset *cs = [nscharacterset newlinecharacterset]; nsscanner *scanner = [nsscanner scannerwithstring:importtxt]; nsstring *line; while (![scanner isatend]) { if ([scanner scanuptocharactersfromset:cs intostring:&line]) { nsstring *copy = [nsstring stringwithstring:line]; nslog(@"copy: %@",copy); [importedtext addobject:copy]; //[newmanagedobject setvalue:copy forkey:@"name"]; } } //------------ save array newarray = [[nsarray alloc] initwitharray:importedtext]; nslog(@"array: %@", [newarray description]); } //--------------- save core info [self savetodatabase]; } -(void)savetodatabase{ nsmanagedobjectcontext *context = [self.fetchedresultscontroller managedobjectcontext]; nsentitydescription *entity = [[self.fetchedresultscontroller fetchrequest] entity]; nsmanagedobject *newmanagedobject = [nsentitydescription insertnewobjectforentityforname:[entity name] inmanagedobjectcontext:context]; (int count = 0; count < [newarray count]; count ++){ [newmanagedobject setvalue:[newarray objectatindex:count] forkey:@"name"]; nslog(@"for() function array object: %@", [newarray objectatindex:count]); // save context. nserror *error = nil; if (![context save:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } else{ nslog(@"save succeeded"); } } }
how can whole array imported core info please?
i tried different methods, saves lastly object only:
nsenumerator *e = [newarray objectenumerator]; id object; while (object = [e nextobject]) { nslog(@"enum: %@", object); [newmanagedobject setvalue:object forkey:@"name"]; }
or
for (nsstring *object in newarray) { [newmanagedobject setvalue:object forkey:@"name"]; nslog(@"for loop: %@", object); }
thank advice
because save values same nsmanagedobject. need insert new 1 in loop.
xcode core-data for-loop import nsarray
Comments
Post a Comment