PFObject compare:options:range 搜索时出错 parse.com class

PFObject compare:options:range error when searching parse.com class

我正在尝试通过 UISearchBar 在我的集合视图中从 Parse.com class 进行搜索查询。当我尝试搜索时,它崩溃了,并给出了以下错误。有没有更好的方法来对集合视图进行搜索功能,如果没有,我做错了什么?

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。例如,如果您的搜索功能正在搜索 "name",您应该与名称进行比较:

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

告诉我这对你有用。