I'm attempting to pull records from Core Data. I can successfully do so minus the fact that I always receive the below error when trying to place the queried elements in a UITableView. Please let me know if you need more information. I think the issue is that I'm not using the proper type of data structure to populate the feed with.
Error:
<NSManagedObject: 0x7ff4cb712f10> (entity: Item; id: 0xd000000000040000 <x-coredata://B5B03BED-0A3E-45EA-BC52-92FB77BE0D51/Item/p1> ; data: <fault>)
2015-04-05 20:29:17.080 TacticalBox[99411:6444447] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7ff4cb71a760
2015-04-05 20:29:17.114 TacticalBox[99411:6444447] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x7ff4cb71a760'
Code:
@property (strong, nonatomic) NSArray *items;
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
// Do any additional setup after loading the view.
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError *error;
items = [context executeFetchRequest:request error:&error];
for(id obj in items)
{
NSLog(@"%@",obj);
}
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [items valueForKey:@"item_name"];
return cell;
}
Your FetchRequest returns you an array of "Item" objects which is stored at every index of your collection.
While accessing these objects you are supposed to first fetch the item from respective index such as
Item* anItem = [items objectAtIndex:indexPath.row];
followed by
cell.textLabel.text = [anItem valueForKey:@"item_name"];
Where item_name is declared property on anItem
object.
As a safety check you can also use -
id anItem = [items objectAtIndex:indexPath.row];
if ([anItem isKIndOfClass:[Item class]]) {
cell.textLabel.text = [(Item*)anItem valueForKey:@"item_name"];
}
but this is absolutely redundant as your FetchQuery explicitly specifies that your items
will have objects of class Item