PFObject比较:选项:searchparse.com类时出现范围错误

我试图通过UISearchBar在我的集合视图中从Parse.com类进行search查询。 当我尝试search它崩溃,并给我下面的错误。 有没有更好的方法来做一个searchfunction的集合视图,如果没有,我做错了什么?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFObject compare:options:range:]: unrecognized selector sent to instance 0x1741366c0' 

代码如下:

 @implementation DiscoverViewController - (void)viewDidLoad { [super viewDidLoad]; filteredContentList = [[NSMutableArray alloc] init]; [self fetchFeatured]; _featured = [[NSMutableArray alloc] initWithCapacity:1000]; } - (void)fetchFeatured { PFQuery *query = [PFQuery queryWithClassName:@"Featured"]; [query findObjectsInBackgroundWithBlock:^(NSArray *posts, NSError *error) { if (!error) { } else { NSLog(@"Error fetching featured"); } [_featured setArray:posts]; [_collectionView reloadData]; }]; } #pragma mark <UICollectionViewDataSource> - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (isSearching) { return [filteredContentList count]; } else { return [self.featured count]; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (isSearching) { DiscoverTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverTableViewCell" forIndexPath:indexPath]; _featuredObject = [_featured objectAtIndex:indexPath.row]; cell.name.text = _featuredObject[@"name"]; [(PFFile*)_featuredObject[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { cell.profilePic.image = [UIImage imageWithData:data]; }]; return cell; } else { DailyDiscoverTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DailyDiscoverTableViewCell" forIndexPath:indexPath]; _featuredObject = [_featured objectAtIndex:indexPath.row]; cell.name.text = _featuredObject[@"name"]; [(PFFile*)_featuredObject[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { cell.profilePic.image = [UIImage imageWithData:data]; }]; return cell; } } - (void)searchTableList { NSString *searchString = _searchBar.text; for (NSString *tempStr in _featured) { NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])]; if (result == NSOrderedSame) { [filteredContentList addObject:tempStr]; } } } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { isSearching = YES; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { [filteredContentList removeAllObjects]; if([searchText length] != 0) { isSearching = YES; [self searchTableList]; } else { isSearching = NO; } [_collectionView reloadData]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@"Cancel clicked"); } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSLog(@"Search Clicked"); [self searchTableList]; } @end 

根据你的代码块:

 for (NSString *tempStr in _featured) { NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])]; if (result == NSOrderedSame) { [filteredContentList addObject:tempStr]; } } 

我想象_featured数组是PFObjects的数组。 它看起来像你试图隐式地PFObject到一个NSString。 如果您的searchfunction正在search“名称”,例如,您应该进行比较以命名:

 for (PFObject *tempObj in _featured) { // or perhaps Featured NSComparisonResult result = [tempObj[@"name"] compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])]; if (result == NSOrderedSame) { [filteredContentList addObject:tempObj]; } } 

让我知道这对你有用。