Search code examples
iosuitableviewnsmutablearraynspredicatensurl

NSPredicate with NSURL crash in iOS


I need to search pdf file that located in document directory with UISearchBar.

I am using this method to load PDF from document directory to show in UITableView.

- (NSArray *)loadBookFromDocumentDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [NSString stringWithFormat:@"%@",[paths objectAtIndex:0]];

    NSFileManager *manager = [NSFileManager defaultManager];

    NSError *error;

    NSArray *files = [manager contentsOfDirectoryAtURL:[NSURL fileURLWithPath:documentsDirectory]
                            includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
                                               options:NSDirectoryEnumerationSkipsHiddenFiles

                                                 error:&error];
    NSArray* sortArray = [files sortedArrayUsingComparator:
                      ^(NSURL *file1, NSURL *file2)
                      {
                          NSDate *file1Date;
                          [file1 getResourceValue:&file1Date forKey:NSURLContentModificationDateKey error:nil];

                          NSDate *file2Date;
                          [file2 getResourceValue:&file2Date forKey:NSURLContentModificationDateKey error:nil];

                          return [file2Date compare: file1Date];
                      }];

    return sortArray;
}

I can successfully show all of pdf document in UITableView. However when i try to search with following code , my app crash and showing this messages.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection file:///Users/User/Library/Application%20Support/iPhone%20Simulator/7.1-64/Applications/D0191F36-D11A-47DF-9274-BE2C72B9C8E1/Documents/%E1%80%B1%E1%80%A1%E1%80%AC%E1%80%B9%E1%80%90%E1%80%AD%E1%80%AF%20Desk%203D%20%E1%80%B1%E1%80%9C%E1%80%B7%E1%80%80%E1%80%BA%E1%80%84%E1%80%B7%E1%80%B9%E1%80%81%E1%80%94%E1%80%B9%E1%80%B8.pdf (not a collection)'

Here is my code that in textDidChange event.

self.filterArray = [[NSMutableArray alloc] init];
        [self.filterArray removeAllObjects];

        NSPredicate *filter = [NSPredicate predicateWithFormat:@"self Contains[cd] %@",searchText];

        self.filterArray = (NSMutableArray *)[self.arrayOfBooks filteredArrayUsingPredicate:filter];

and this is UITableViewCell code.

NSURL *url = [self.filterArray objectAtIndex:indexPath.row];

        cell.textLabel.text = [[[[url absoluteString] lastPathComponent] stringByReplacingOccurrencesOfString:@"+" withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

How can i solve it? Please help me.


Solution

  • I guess you'll need to change the URLs to strings to run the predicate. Try:

    [NSPredicate predicateWithFormat:@"absoluteString Contains[cd] %@",searchText];
    

    I get that you want URLs to begin with so you can cache the dates, but after that you appear to only want strings so you could use valueForKey:@"absoluteString" on sortArray and then you wouldn't need to worry about the URL conversion in follow on code.