10

我讀過關於這次崩潰有關與謂詞返回零有關的其他職位,但即時通訊無法與我的應用程序弄清楚。有人可以幫助我嗎?NSInvalidArgumentException',原因:'無效的謂詞:無RHS,需要幫助搞清楚這

static NSString *const KJMWorkoutCategorySectionKeyPath = @"workoutCategory"; 

- (NSFetchedResultsController *)fetchedResultsControllerWithSearchString:(NSString *)searchString { 
    NSManagedObjectContext *sharedContext; // my NSManagedObjectContext instance... 

    NSFetchRequest *request = [NSFetchRequest new]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Workouts" 
               inManagedObjectContext:sharedContext]; 
    request.entity = entity; 
    request.predicate = [NSPredicate predicateWithFormat:@"(workoutName CONTAINS[cd] %@)", searchString]; 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:KJMWorkoutCategorySectionKeyPath ascending:YES]; 
    request.sortDescriptors = @[sortDescriptor]; 

    NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                           managedObjectContext:sharedContext 
                           sectionNameKeyPath:KJMWorkoutCategorySectionKeyPath 
                              cacheName:nil]; 
    fetchedResultsController.delegate = self; 

    NSError *error = nil; 

    if (![fetchedResultsController performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, error.userInfo); 
     abort(); 
    } 

    return fetchedResultsController; 
} 
+0

它是否在'predicateWithFormat'行或'set謂詞線? –

+0

它正好在下面的行崩潰:'if(![aFetchedResultsController performFetch:&error])' – kevnm67

回答

21

的錯誤信息表明searchStringnil

NSPredicate *filterPredicate = [NSPredicate 
      predicateWithFormat:@"(workoutName CONTAINS[cd] %@)", searchString]; 

如果意圖是,如果沒有搜索字符串給顯示所有對象,你應該 只是沒有謂詞分配給在這種情況下獲取請求:

if ([searchString length] > 0) { 
    NSPredicate *filterPredicate = [NSPredicate 
      predicateWithFormat:@"(workoutName CONTAINS[cd] %@)", searchString]; 
    [request setPredicate:filterPredicate]; 
} 
+1

謝謝Martin!這似乎工作。 – kevnm67

+1

@ kevnm67:不客氣! –

相關問題