Search code examples
iphoneobjective-cxcodensfetchrequest

NSFetchRequest return value


I can't figure out why this code returns an error

- (void) deleteCategoriesWithNoProducts{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.itemDatabase.managedObjectContext];
    [request setEntity:entity]; 

    //NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Category"];


    NSSortDescriptor *sortDescriptor = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];    

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error;
    NSArray *fetchResults = [self.itemDatabase.managedObjectContext executeFetchRequest:request error:&error];


}

Here's the error:

-[__NSArrayI key]: unrecognized selector sent to instance 0x8267ad0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI key]: unrecognized selector sent to instance 0x8267ad0'

Hope somebody can help me out. Thanks!


Solution

  • Here's your mistake:

    NSSortDescriptor *sortDescriptor = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];    
    

    Should be:

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];    
    

    That line should have been giving you a warning in the compiler, too.