我在UISearchBar
内有一个UITableView
,当点击它并开始输入时,它会自动更新列表。我有一个名为_cloudItems
的NSMutableArray,它返回一个JSON(我正在使用JJCloudEngine来检索JSON字符串。)
项目中的大多数代码都很好,期望这段代码给我这个错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI rangeOfString:options:]: unrecognized selector sent to instance 0x17e343e0'
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
// Set bollean flag
isFiltered = NO;
} else {
// Set boolean flag
isFiltered = YES;
// Alloc and init our fliteredData
filteredList = [[NSMutableArray alloc] init];
// Fast enumeration
for (NSString *name in _cloudItems) {
NSRange nameRange = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[filteredList addObject:name];
}
}
}
// Reload tableView
[table reloadData];
}
我知道这两行代码是导致问题的原因:
for (NSString *name in _cloudItems) {
if (nameRange.location != NSNotFound) {
注意:_cloudItems
是NSMutableArray
,并从此代码块中获取代码:
- (void)itemsRecieved:(NSArray *)items forRequest:(NSString *)connectionId {
NSLog(@"cloud engine recieved items on request %@", connectionId);
_cloudItems = items;
[table reloadData];
}
我尝试将for (NSString *name in _cloudItems) {
更改为NSArray
和NSMutableArray
,但它们无效。我还补充说:
NSDictionary *dictItem = [_cloudItems objectAtIndex:indexPath.row];
for (NSString *name in dictItem) {
但是会出现以下错误消息:
Use of undeclared identifier 'indexPath'; did you mean 'NSIndexPath'?
我做错了什么?任何帮助表示赞赏。谢谢。
答案 0 :(得分:3)
试试这个
for (id name in _cloudItems) {
if([name isKindOfClass:[NSString class]]){
NSRange nameRange = [(NSString *)name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[filteredList addObject:name];
}
}
}
您应确认名称为NSString
,然后使用rangOfString
。数组中的某些对象为NSDictionary
。如果所有对象都是NSDictionary
,您可能需要以下内容:
for (NSDictionary *item in _cloudItems) {
NSString *name = [item objectForKey:@"somekey"];
NSRange nameRange = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[filteredList addObject:name];
}
}
答案 1 :(得分:0)
在_clouditems中你的一个名字可能不是[NSString],是[NSDictionary],[NSDictionary]不响应rangeOfString:options :.在句子上设置断点,这样就可以检查名称变量。