2014-03-30 76 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