搜索parse.com类时PFObject compare:options:range错误 [英] PFObject compare:options:range error when searching parse.com class

查看:64
本文介绍了搜索parse.com类时PFObject compare:options:range错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a search query from a Parse.com class, in my collection view, through a UISearchBar. When I try to search it crashes and gives me the error below. Is there a better way to make a search function to a collection view, and if not, what am I doing wrong?

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

I imagine that the _featured array is an array of PFObjects. It looks like you are trying to implicitly cast the PFObject to an NSString. If your search functionality is searching for "name", for example, you should do your comparison to 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];
    }
}

让我知道它如何为您工作.

Let me know how that works for you.

这篇关于搜索parse.com类时PFObject compare:options:range错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆