NSFetchRequest возвращаемое значение - PullRequest
1 голос
/ 01 марта 2012

Я не могу понять, почему этот код возвращает ошибку

- (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];


}

Вот ошибка:

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

Надеюсь, кто-нибудь может мне помочь. Спасибо!

1 Ответ

3 голосов
/ 01 марта 2012

Вот ваша ошибка:

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

Должно быть:

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

Эта строка тоже должна была давать вам предупреждение в компиляторе.

...