在搜索栏

时间:2016-08-09 07:09:54

标签: ios objective-c uisearchbardelegate

enter image description here这里我已经实现了使用搜索栏所需的所有内容,但不知道应用程序出了什么问题。当我开始在搜索栏中输入任何内容时,它会崩溃。任何帮助表示赞赏。

错误:

'Can't use in/contains operator with collection <NSManagedObject: 0x7fa1c24d32a0> (entity: Personal; id: 0xd0000000000c0000 <x-coredata://2C964E4A-A8CD-465B-BB23-0121B7C7EEFB/Personal/p3> ; data: {

attribute = Ios;
name = man;
}) (not a collection)'

*** First throw call stack:
(
    0   CoreFoundation                      0x0000000104ed5e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001045c2deb objc_exception_throw + 48
    2   Foundation                          0x00000001041db6b1 -[NSCompoundPredicate evaluateWithObject:substitutionVariables:] + 0
    3   Foundation                          0x00000001041b2719 -[NSPredicateOperator performOperationUsingObject:andObject:] + 286
    4   Foundation                          0x00000001041b1e3c -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 313
    5   Foundation                          0x00000001041b1c94 _filterObjectsUsingPredicate + 398
    6   Foundation                          0x00000001041b1a84 -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 279
    7   Personal Data                       0x00000001040ba6a2 -[ViewDetailViewController updateSearchResultsForSearchController:] + 514
    8   UIKit                               0x0000000105cb2185 -[UISearchController _searchBar:textDidChange:] + 102
    9   UIKit                               0x0000000105675d47 -[UISearchBar(UISearchBarStatic) _searchFieldEditingChanged] + 360
    10  UIKit                               0x0000000105285194 -[UIApplication sendAction:to:from:forEvent:] + 92
    11  UIKit                               0x00000001053f46fc -[UIControl sendAction:to:forEvent:] + 67
    12  UIKit                               0x00000001053f49c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
    13  UIKit                               0x0000000105c68a85 -[UITextField fieldEditorDidChange:] + 206
    14  UIKit                               0x0000000105c73cb7 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 138
    15  UIKit                               0x0000000105c76281 -[UITextInputController _insertText:fromKeyboard:] + 893
    16  UIKit                               0x0000000105c76bcd -[UITextInputController insertText:] + 365
    17  UIKit                               0x0000000105400728 -[UIFieldEditor insertFilteredText:] + 800
    18  UIKit                               0x0000000105c6d7d7 -[UITextField insertFilteredText:] + 103
    19  UIKit                               0x000000010552ba83 -[UIKeyboardImpl insertText:] + 125
    20  UIKit                               0x0000000105528278 -[UIKeyboardImpl performKeyboardOutput:] + 496
    21  UIKit                               0x0000000105527e65 __55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 + 164
    22  UIKit                               0x0000000105c96724 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 332
    23  Foundation                          0x00000001041aa067 __NSThreadPerformPerform + 283
    24  CoreFoundation                      0x0000000104e01a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    25  CoreFoundation                      0x0000000104df795c __CFRunLoopDoSources0 + 556
    26  CoreFoundation                      0x0000000104df6e13 __CFRunLoopRun + 867
    27  CoreFoundation                      0x0000000104df6828 CFRunLoopRunSpecific + 488
    28  GraphicsServices                    0x00000001086f0ad2 GSEventRunModal + 161
    29  UIKit                               0x0000000105283610 UIApplicationMain + 171
    30  Personal Data                       0x00000001040bbd9f main + 111
    31  libdyld.dylib                       0x000000010762992d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException



 #import "ViewDetailViewController.h"
    #import <CoreData/CoreData.h>

    @interface ViewDetailViewController ()  <UITableViewDelegate,UITableViewDataSource>

       {
      NSMutableArray *_park;
       }

        @property (nonatomic, strong) NSMutableArray *searchResult;

       @end

       @implementation ViewDetailViewController

     - (NSManagedObjectContext *)managedObjectContext
     {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
     }

       - (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Personal"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

搜索栏声明

[self.tableView reloadData];

    //   self.searchResult = [NSMutableArray arrayWithCapacity:       [self.devices count]];

    searchResultsArray  = [[NSArray alloc]init];

    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchController.searchBar.delegate = self;
    self.searchController.searchResultsUpdater = self;
    [self.searchController.searchBar sizeToFit];
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    self.tableView.tableHeaderView = self.searchController.searchBar;


    // Do any additional setup after loading the view.
        }

      -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{

    NSString *searchString = self.searchController.searchBar.text;
    if (searchString.length > 0)
       {
        NSPredicate *resultPredicate;
        NSInteger scope = self.searchController.searchBar.selectedScopeButtonIndex;

        if(scope == 0){
            resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];
        }else{
            resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];
        }

        NSArray * filterArray = [self.devices filteredArrayUsingPredicate:resultPredicate];

        _park = [[NSMutableArray alloc] init];

        [_park addObject:filterArray];


        }
    else{

        //_park = (NSMutableArray *)shortedArray;
       }
    [self.tableView reloadData];

     }


          - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{

    [self updateSearchResultsForSearchController:self.searchController];

       }

TableView委托方法

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {

    return 1;
       }

           - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {

        return [self.devices count];

         }

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

       // Configure the cell...
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];


         // cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];

          [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]];

       [cell.detailTextLabel setText:[device valueForKey:@"attribute"]];

      return cell;
        }

      }

1 个答案:

答案 0 :(得分:1)

试试这个,在SELF中使用属性名称。我假设对象是Personal类型的属性名称和其他。

NSPredicate *resultPredicate;
NSInteger scope = self.searchController.searchBar.selectedScopeButtonIndex;

if(scope == 0){
    resultPredicate = [NSPredicate predicateWithFormat:@"(SELF.name contains[cd] %@) OR (SELF.other CONTAINS[cd] %@)",searchString, searchString];
}else{
    resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@",searchString];
}

NSArray * filterArray = [self.devices filteredArrayUsingPredicate:resultPredicate];

_park = [[NSMutableArray alloc] init];

[_park addObject:filterArray];