问题
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'
Here's the code:
@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:
Based on your code block:
for (NSString *tempStr in _featured) {
NSComparisonResult result = [tempStr compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[filteredContentList addObject:tempStr];
}
}
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.
来源:https://stackoverflow.com/questions/29783157/pfobject-compareoptionsrange-error-when-searching-parse-com-class