2014-03-30 139 views
-1

我试图使搜索过滤器为我的tableview工作,它崩溃了应用程序,当我点击搜索栏,并返回此错误这个错误是什么意思?

2014-03-30 14:44:08.676书店[16156:90b] *终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: ' - [PFObject rangeOfString:选项:]:无法识别的选择发送到实例0x9da1a40' *第一掷调用堆栈:

这里是代码:

// 
// PDFTableViewController.m 
// BookStore 
// 
// Created by Danijel Kujundzic on 3/23/14. 
// Copyright (c) 2014 Danijel Kujundzic. All rights reserved. 
// 

#import "PDFTableViewController.h" 
#import "PDFDetailViewController.h" 
@interface PDFTableViewController()<UISearchBarDelegate , UISearchDisplayDelegate> 
{ 
    NSMutableArray * filteredStrings; 
    BOOL isFiltered; 
} 

@end 

@implementation PDFTableViewController 




@synthesize PDFtableView; 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self performSelector:@selector(RetrievePDFParseData)]; 
    self.SearchBar.delegate =self; 
    self.PDFtableView.delegate=self; 
    self.PDFtableView.dataSource =self; 
    filteredStrings = [[NSMutableArray alloc] initWithArray:PDFArray]; 
} 



- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if (searchText.length ==0) { 
     isFiltered= NO; 
     [filteredStrings removeAllObjects]; 
     [self.tableView reloadData]; 
     [searchBar resignFirstResponder]; 
    } 
    else 
    { 
     isFiltered = YES; 
     if([PDFArray count]!=0) 
     { 
      NSPredicate *p=[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { 
       NSString *s=evaluatedObject; 
       return ([s rangeOfString:searchBar.text options:NSCaseInsensitiveSearch].location !=NSNotFound); 
      }]; 
      filteredStrings= [NSMutableArray arrayWithArray:[PDFArray filteredArrayUsingPredicate:p]]; 
      //table reload 
      [self.tableView reloadData]; 
     } 

    } 
} 




-(void) RetrievePDFParseData { 
    PFQuery * getPDF = [PFQuery queryWithClassName:@"PDFTableView"]; 

    [getPDF findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if(!error) { 
      PDFArray =[[NSArray alloc] initWithArray: objects]; 

     } 
     [PDFtableView reloadData]; 

    }]; 

} 




- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - Table view data source 





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (isFiltered) { 
     return [filteredStrings count]; 
    } 


    return [PDFArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString * CellIdentifier = @"Cell"; 
     UITableViewCell * cell = [PDFtableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell ==nil) { 
      cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; 
     } 

     if (!isFiltered) { 
      PFObject * tempObject = [PDFArray objectAtIndex:indexPath.row]; 
      cell.textLabel.text = [tempObject objectForKey:@"PDFName"]; 
      cell.detailTextLabel.text= [tempObject objectForKey:@"Author"]; 
     } 

     if (isFiltered) 


     { 
      PFObject *filteredObject= [[filteredStrings objectAtIndex:indexPath.row]initWithArray:PDFArray]; 
      cell.textLabel.text =[filteredObject objectForKey:@"PDFName"]; 
      cell.detailTextLabel.text= [filteredObject objectForKey:@"Author"]; 
      NSLog(@"%@", filteredObject); 
     } 


     return cell; 

    } 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self performSegueWithIdentifier:@"detailSegue" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

    if([segue.identifier isEqualToString:@"detailSegue"]){ 

     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     PDFDetailViewController *detailVC = (PDFDetailViewController *)segue.destinationViewController; 
     NSLog(@"Bookarray=%@", PDFArray); 
     NSLog(@"BookIndex=%@", [PDFArray objectAtIndex:indexPath.row]); 
     detailVC.PDFna=[[PDFArray objectAtIndex:indexPath.row]objectForKey:@"PDFName"]; 
     detailVC.PDFdes= [[PDFArray objectAtIndex:indexPath.row]objectForKey:@"About"]; 
     detailVC.downloadfile=[[PDFArray objectAtIndex:indexPath.row]objectForKey:@"PDFFile"]; 


    } 
} 


@end 

回答

1

您认为是NSStrings的对象数组实际上是PFObjects。 PDFArray包含一个PFObject的数组。你可能想抓住你在你的UISearchBarDelegate方法创建的断言某个属性,

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

喜欢的东西NSString *s = evaluatedObject[@"PDFName"];,或诸如此类的话将让这个你实际上是沿着名称筛选的PDF文件。

+0

我只是去尝试,而不是崩溃的细胞在“过滤”选项实例行方法0x9a173e0 2014年3月30日14:59:58.614 BookStore [16310:90b] ***由于未捕获异常'NSInvalidArgumentException',原因:' - [PFObject initWithArray:]:无法识别的选择器发送到实例0x9a173e0' – DannyK

+0

Ahh man你是一个王! !你甚至不明白我花了多长时间来做这件事......我爱你的男人! – DannyK

1

错误出现在searchBar:textDidChange:方法中。谓词在这种情况下返回PFObject,而不是NSString。因此,您必须在evaluatedObject对象中搜索您需要的NSString对象。

这些都是有问题的线路:

NSString *s=evaluatedObject; 
return ([s rangeOfString:searchBar.text options:NSCaseInsensitiveSearch].location !=NSNotFound); 

抛出异常的rangeOfString,因为PFObject并不rangeOfString回应。

在其他语言中,编译器有时会警告您这种情况。在Objective-C中,处理任何类型的对象时必须小心:id。编译器不会检查特定对象是否在编译时响应该消息。但是,当Objective-C运行时在运行时执行此操作时,会发生崩溃。

了解更多关于对象和消息在Objective-C: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html