当我在搜索栏中输入第一个字符时,我的应用程序崩溃了

Posted

技术标签:

【中文标题】当我在搜索栏中输入第一个字符时,我的应用程序崩溃了【英文标题】:when i enter first character in search bar my app crashes 【发布时间】:2014-08-06 08:04:24 【问题描述】:

我已经在 tableview 中解析了JSON,我想实现搜索栏来过滤大量数据。我已经尝试从here 实现搜索栏。

但它崩溃了。

-(void)parsingmethod

   NSString *string = [NSString stringWithFormat:@"HEREISMYJSONURL"];
   NSURL *url = [NSURL URLWithString:string];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
   operation.responseSerializer = [AFJSONResponseSerializer serializer];
   [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 

   self.json=[responseObject valueForKey:@"name"];
   self.filteredCandyArray = [NSMutableArray arrayWithCapacity:[candyArray count]];
   self.filteredArray=[NSMutableArray arrayWithArray:self.json];
   NSLog(@"self.flteredarray=%@",self.filteredArray);
   self.current_address = [responseObject valueForKey:@"current_address"];

   [self.tableView reloadData];

   failure:^(AFHTTPRequestOperation *operation, NSError *error)
    NSLog(@"Oops, something went wrong: %@", [error localizedDescription]);
  ];
  [operation start]; 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if ( cell == nil ) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    if (tableView == self.searchDisplayController.searchResultsTableView) 
        cell.textLabel.text =[self.filteredArray objectAtIndex:indexPath.row];
     else 
        cell.textLabel.text =[self.json objectAtIndex:indexPath.row];
    
    cell.detailTextLabel.text=[self.current_address objectAtIndex:indexPath.row];
    return cell;

并且内容过滤和委托方法也是从here完成的

在我的崩溃日志下方

  ***** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0xcc515f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
    0   CoreFoundation                      0x021af1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0199c8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x0223efe1 -[NSException raise] + 17
    3   Foundation                          0x0165cc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
    4   Foundation                          0x015c9dfd _NSGetUsingKeyValueGetter + 81
    5   Foundation                          0x015c9437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260
    6   Foundation                          0x0159a465 -[NSFunctionExpression expressionValueWithObject:context:] + 1260
    7   Foundation                          0x016202c1 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 248
    8   Foundation                          0x016201c1 -[NSPredicate evaluateWithObject:] + 48
    9   Foundation                          0x01620141 _filterObjectsUsingPredicate + 418
    10  Foundation                          0x0161fefa -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 328
    11  LuckyHR                             0x00043995 -[NameListTableViewController filterContentForSearchText:scope:] + 245
    12  LuckyHR                             0x00043bf2 -[NameListTableViewController searchDisplayController:shouldReloadTableForSearchString:] + 386
    13  UIKit                               0x00a1eb37 -[UISearchDisplayController searchBar:textDidChange:] + 128
    14  UIKit                               0x0093dc56 -[UISearchBar(UISearchBarStatic) _searchFieldEditingChanged] + 178
    15  libobjc.A.dylib                     0x019ae82b -[NSObject performSelector:withObject:] + 70
    16  UIKit                               0x0065e3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    17  UIKit                               0x0065e345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    18  UIKit                               0x0075fbd1 -[UIControl sendAction:to:forEvent:] + 66
    19  UIKit                               0x0075ffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    20  UIKit                               0x00d8529d -[UITextField fieldEditorDidChange:] + 221
    21  UIKit                               0x00765db4 -[UIFieldEditor textInputDidChange:] + 58
    22  UIKit                               0x00d931b3 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 118
    23  UIKit                               0x00d95090 -[UITextInputController _insertText:fromKeyboard:] + 768
    24  UIKit                               0x00d95954 -[UITextInputController insertText:] + 372
    25  UIKit                               0x00768a64 -[UIFieldEditor insertText:] + 1086
    26  UIKit                               0x00d88ffa -[UITextField insertText:] + 59
    27  UIKit                               0x00848760 -[UIKeyboardImpl insertText:] + 87
    28  UIKit                               0x00859ea4 -[TIKeyboardOperationInsertText(UIKeyboardImpl) main] + 83
    29  Foundation                          0x01670c79 -[__NSOperationInternal _start:] + 671
    30  Foundation                          0x015ed9c8 -[NSOperation start] + 83
    31  UIKit                               0x00846d4d -[UIKeyboardImpl performOperations:] + 153
    32  UIKit                               0x00844f8e -[UIKeyboardImpl continueHandleKeyboardInputWithOperations:] + 75
    33  UIKit                               0x00844ddc __73-[UIKeyboardImpl replyHandlerForHandleKeyboardInputWithExecutionContext:]_block_invoke_2 + 44
    34  UIKit                               0x00dae978 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 402
    35  libobjc.A.dylib                     0x019ae82b -[NSObject performSelector:withObject:] + 70
    36  Foundation                          0x015f0e48 __NSThreadPerformPerform + 285
    37  CoreFoundation                      0x0213877f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    38  CoreFoundation                      0x0213810b __CFRunLoopDoSources0 + 235
    39  CoreFoundation                      0x021551ae __CFRunLoopRun + 910
    40  CoreFoundation                      0x021549d3 CFRunLoopRunSpecific + 467
    41  CoreFoundation                      0x021547eb CFRunLoopRunInMode + 123
    42  GraphicsServices                    0x031d45ee GSEventRunModal + 192
    43  GraphicsServices                    0x031d442b GSEventRun + 104
    44  UIKit                               0x0065cf9b UIApplicationMain + 1225
    45  LuckyHR                             0x000499fd main + 141
    46  libdyld.dylib                       0x0392a70d start + 1
    47  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) *

我去了这里发布的类似问题,但仍然没有弄清楚。

【问题讨论】:

请正确格式化您的代码并包含您遇到的错误 【参考方案1】:

写得很清楚:

3   Foundation                          0x0165cc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4   Foundation                          0x015c9dfd _NSGetUsingKeyValueGetter + 81
5   Foundation                          0x015c9437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260

您要求一个不存在的密钥,因此它不起作用。

【讨论】:

以上是关于当我在搜索栏中输入第一个字符时,我的应用程序崩溃了的主要内容,如果未能解决你的问题,请参考以下文章

输入第一个字符时 UISearchBar 不起作用?

我的 C 程序不断崩溃

错误 - 在反应搜索组件中呈现多个实例

iOS 搜索栏不显示结果

来自 AppDelegate 的当前 viewController 崩溃/空屏

NSManagedObjectContext -save:使用 NSPredicate 时崩溃