views:

613

answers:

1

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...

-(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

+1  A: 

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];
sfa
thanks for the response. I have been able to load the objects into the array, but I didn't ask the correct question. I am looking to load the objects attribute "eventName" into the array. So I would have an array of strings and not an array of NSManagedObjects.
OscarTheGrouch
Once you have the array of NSManagedObjects, you can do a loop to each item to put only the eventName into another array.
sfa