I have a problem, i have a method that when a button is pressed retrieve the information from the model core data and fill the array with the string retrived in the entity core data selected, but the array is empty, why?...
- (IBAction)action:(id)sender{
int numberanswer = [(NSSet *)[list valueForKey:@"answers"] count];
NSLog(@"The list have got: %i elements.", numberanswer);
[arrayAnswer initWithCapacity:numberanswer];
for (int i =0; i < numberanswer; i++) {
NSManagedObject *answer = [[self sortAnswer] objectAtIndex:i];
NSString *answerString = [[NSString alloc] initWithString:[[answer valueForKey:@"nameAnswer"] description]];
NSLog(@"Answer: %@", answerString);
[arrayAnswer insertObject:answerString atIndex:i];
NSLog(@"%@", [arrayAnswer objectAtIndex:i]);
}
NSLog(@"%@", [arrayAnswer count]);
}
here:
NSLog(@"Risposta: %@", answerString);
i see that the answerString is full at every for cycle, why the array is empty? this is the NSLog of the method: 2011-04-05 23:59:57.538 test[574:207] The list have got: 20 elements.
2011-04-05 23:59:57.540 test[574:207] Answer: This is answer1.
2011-04-05 23:59:57.541 test[574:207] (null)
2011-04-05 23:59:57.541 test[574:207] Answer: This is answer2.
2011-04-05 23:59:57.541 test[574:207] (null)
2011-04-05 23:59:57.542 test[574:207] Answer: This is answer3.
2011-04-05 23:59:57.542 test[574:207] (null)
2011-04-05 23:59:57.543 test[574:207] Answer: This is a开发者_如何学Pythonnswer4.
2011-04-05 23:59:57.543 test[574:207] (null)
2011-04-05 23:59:57.543 test[574:207] Answer: This is answer5.
2011-04-05 23:59:57.544 test[574:207] (null)
2011-04-05 23:59:57.544 test[574:207] Answer: This is answer6.
2011-04-05 23:59:57.544 test[574:207] (null)
2011-04-05 23:59:57.545 test[574:207] Answer: This is answer7.
2011-04-05 23:59:57.545 test[574:207] (null)
2011-04-05 23:59:57.546 test[574:207] Answer: This is answer8.
2011-04-05 23:59:57.546 test[574:207] (null)
2011-04-05 23:59:57.546 test[574:207] Answer: This is answer9.
2011-04-05 23:59:57.547 test[574:207] (null)
2011-04-05 23:59:57.547 test[574:207] Answer: This is answer10.
2011-04-05 23:59:57.547 test[574:207] (null)
2011-04-05 23:59:57.548 test[574:207] Answer: This is answer11.
2011-04-05 23:59:57.548 test[574:207] (null)
2011-04-05 23:59:57.548 test[574:207] Answer: This is answer12.
2011-04-05 23:59:57.549 test[574:207] (null)
2011-04-05 23:59:57.549 test[574:207] Answer: This is answer13.
2011-04-05 23:59:57.549 test[574:207] (null)
2011-04-05 23:59:57.550 test[574:207] Answer: This is answer14.
2011-04-05 23:59:57.550 test[574:207] (null)
2011-04-05 23:59:57.551 test[574:207] Answer: This is answer15.
2011-04-05 23:59:57.551 test[574:207] (null)
2011-04-05 23:59:57.551 test[574:207] Answer: This is answer16.
2011-04-05 23:59:57.552 test[574:207] (null)
2011-04-05 23:59:57.552 test[574:207] Answer: This is answer17.
2011-04-05 23:59:57.552 test[574:207] (null)
2011-04-05 23:59:57.553 test[574:207] Answer: This is answer18.
2011-04-05 23:59:57.553 test[574:207] (null)
2011-04-05 23:59:57.553 test[574:207] Answer: This is answer19.
2011-04-05 23:59:57.554 test[574:207] (null)
2011-04-05 23:59:57.554 test[574:207] Answer: This is answer20.
2011-04-05 23:59:57.554 test[574:207] (null)
NSString *answerString = [[NSString alloc] initWithString:[[answer valueForKey:@"nameAnswer"] description]];
Shouldn't be necessary. You have already said in your Core Data model that it is a string, so just treat it that way.
NSString *answerString = [answer valueForKey:@"nameAnswer"];
However, I'm not sure why you're getting (null) values. Try [arrayAnswer addObject:answerString] instead.
精彩评论