Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data crash - unrecognized selector

Tags:

ios

core-data

I've seen other posts regarding unrecognized selector errors, but nothing regarding the auto-generated code in the following method:

- (void)tableView:(UITableView *)tableView commitEditingStyle:forRowAtIndexPath:

This is called when I swipe to delete, and when I tap the delete button I get the following error:

-[Vehicle removeObjectFromGasUpsAtIndex:]: unrecognized selector sent to instance 0x8172c60

My NSManagedObject class, Vehicle, has a NSOrderedSet variable named gasUps that should respond to this message when I want to delete it. Or so I thought.

Here is the entire method where it is crashing:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (editingStyle == UITableViewCellEditingStyleDelete) {
    // need to delete gas up and not vehicle
    int row = [indexPath row];

    //[self.selectedVehicle removeObjectFromGasUpsAtIndex:row];
    [self.selectedVehicle removeObjectFromGasUpsAtIndex:row];
    NSError *error = nil;
    if (![self.managedObjectContext save:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
 }   
}

Why is this selector unrecognized?

EDIT: this is for Jody.

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

typedef enum {
    FullExtract,
    NameExtract
} kTitleExtract;

@interface Vehicle : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * year;
@property (nonatomic, retain) NSString * make;
@property (nonatomic, retain) NSNumber * mileage;
@property (nonatomic, retain) NSString * model;
@property (nonatomic, retain) NSOrderedSet *gasUps;

// added methods
- (NSString *)getTitleExtract:(kTitleExtract)titleExtract;

@end

@interface Vehicle (CoreDataGeneratedAccessors)

- (void)insertObject:(NSManagedObject *)value inGasUpsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromGasUpsAtIndex:(NSUInteger)idx;
- (void)insertGasUps:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeGasUpsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInGasUpsAtIndex:(NSUInteger)idx withObject:(NSManagedObject *)value;
- (void)replaceGasUpsAtIndexes:(NSIndexSet *)indexes withGasUps:(NSArray *)values;
- (void)addGasUpsObject:(NSManagedObject *)value;
- (void)removeGasUpsObject:(NSManagedObject *)value;
- (void)addGasUps:(NSOrderedSet *)values;
- (void)removeGasUps:(NSOrderedSet *)values;

@end

And here is the snippet of code where self.selectedVehicle is set:

if (self.vehiclesArray != nil  && [self.vehiclesArray count] != 0 && self.vehicleIndex != -1) {
    // enable add buttom
    [self.navigationItem.rightBarButtonItem setEnabled:YES];
    self.selectedVehicle = [self.vehiclesArray objectAtIndex:self.vehicleIndex];
    NSLog(@"set selected vehicle");
} else {
    // disable add button
    [self.navigationItem.rightBarButtonItem setEnabled:NO];

    // set vehicle defaults to -1 and make disable add buttom
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:-1 forKey:@"vehicleIndex"];
    [defaults synchronize];
    NSLog(@"set index to -1");
}
like image 217
kschins Avatar asked Sep 12 '12 00:09

kschins


1 Answers

This is probably related to a known bug in the Core Data auto generated accessors for ordered one-to-many relationships.

rdar://10114310

Until this is addressed you need to provide your own implementation for the missing accessors.

See answers to this question

like image 68
FluffulousChimp Avatar answered Sep 30 '22 12:09

FluffulousChimp