PFObject比较:选项:搜索parse.com类时的范围错误

时间:2015-04-21 21:09:05

标签: ios parse-platform uicollectionview uisearchbar

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

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

1 个答案:

答案 0 :(得分:1)

基于您的代码块:

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。例如,如果您的搜索功能正在搜索&#34; name&#34;,那么您应该对名称进行比较:

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

让我知道这对你有用。