kschins
kschins

Reputation: 1224

Core Data crash - unrecognized selector

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");
}

Upvotes: 5

Views: 3352

Answers (1)

FluffulousChimp
FluffulousChimp

Reputation: 9185

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

Upvotes: 5

Related Questions