iPhone Core Data objects added to NSMutableArray
I am trying to have a bunch of Core Data objects (NSStrings) be added to an NSMutableArray so that I pickerView can use the strings as its data values.
Currently I am able to save strings from a textField one at a time into the sqlite file, in Core Data.
I am having trouble pulling those Objects back out of the Core Data sqlite file and into a NSMutable array.
This is the code to add objects to the sqlite file开发者_StackOverflow社区...
-(IBAction)addToPicker
{
CoreDataPickerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *thePerson = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
[thePerson setValue:textField.text forKey:@"eventName"];
[context save:&error];
textField.text=@"";
self.viewDidLoad;
[textField resignFirstResponder];
}
Right now I have...
- (void)viewDidLoad {
NSMutableArray *array = [[NSMutableArray alloc] init];
//code needed to send all the objects in the sqlite Core Data store into array to be read by the pickerView
self.pickerData = array; // the picker uses NSMutableArray pickerData for its data
[array release];
[super viewDidLoad];
}
any help would greatly be appreciated. Chris
You can use the NSFetchRequest to fetch the data into the array. Take a look at the Core Data Programming guide - Fetching Managed Object. You just need to read the first section of the link.
Executing a fetch request will return an array
NSArray *array = [moc executeFetchRequest:request error:&error];
精彩评论