I want to retrieve a list of types from a "table" from my Main.xcdatamodeld (coredata) to put in a UIPickerView. This entity "Type" has only one attribute "name" (string).
If I put some strings in the NSMutableArray it works perfectly. The UIPickerView shows the strings. Above is the simple NSMutableArray.
resultFetch = [[NSMutableArray alloc]initWithObjects:@"Electronics",@"School",@"Kitchen",@"Office", nil];
But when I do a fetch from the coredata I get an error. Here's the code below.
AddItemViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
nameField.text = [self.currentItem name];
locationField.text = [self.currentItem location];
//typeField.text = [self.currentItem type];
quantityField.text = [[self.currentItem quantity] stringValue];
self.nameField.delegate = self;
self.locationField.delegate = self;
//self.typeField.delegate = self;
self.quantityField.delegate = self;
NSLog(@"ViewDidload before put fetch in array");
//Put fetch in NSArray resultFetch
AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
resultFetch = [[NSMutableArray alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type" inManagedObjectContext:[myApp managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error;
NSArray *fetchedObjects = [[myApp managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (Type *t in fetchedObjects) {
[resultFetch addObject:t];
NSLog(@"resultFetch: %@", resultFetch);
}
//Define typePicker
typePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,43,320,480)];
typePicker.delegate = self;
typePicker.dataSource = self;
[typePicker setShowsSelectionIndicator:YES];
typeField.inputView = typePicker;
//Create done button in UIPickerView
myPickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
myPickerToolbar.barStyle = UIBarStyleBlackOpaque;
[myPickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[myPickerToolbar setItems:barItems animated:YES];
typeField.inputAccessoryView = myPickerToolbar;
NSLog(@"End ViewDidLoad");
}
-(void)pickerDoneClicked
{
NSLog(@"Done Clicked");
[typeField resignFirstResponder];
myPickerToolbar.hidden=YES;
typePicker.hidden=YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [resultFetch count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [resultFetch objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
typeField.text = [resultFetch objectAtIndex:row];
}
The error is:
2013-11-14 10:31:53.099 Storage[1226:70b] ViewDidload before put fetch in array
2013-11-14 10:31:53.102 Storage[1226:70b] resultFetch: ( " (entity: Type; id: 0x8a83e50 ; data: )" )
2013-11-14 10:31:53.103 Storage[1226:70b] resultFetch: ( " (entity: Type; id: 0x8a83e50 ; data: )", " (entity: Type; id: 0x8a6ae80 ; data: )" )
2013-11-14 10:31:53.104 Storage[1226:70b] resultFetch: ( " (entity: Type; id: 0x8a83e50 ; data: )", " (entity: Type; id: 0x8a6ae80 ; data: )", " (entity: Type; id: 0x8aa9650 ; data: )" )
2013-11-14 10:31:53.107 Storage[1226:70b] End ViewDidLoad
2013-11-14 10:31:57.148 Storage[1226:70b] -[Type length]: unrecognized selector sent to instance 0x8aa08e0
It doesn't show the Type names that are inside the database. Can you help me fetching this "table" properly to put those attribute strings inside a UIPickerView? All I want is a pretty NSMutableArray I can use! :)
Thank you very much for your help.
Paula
Problem solved! Changed the line inside for statement from [resultFetch addObject:t]; to [resultFetch addObject:t.name];. Seems it was too much garbage! :)