单击键盘的任何值时出错

时间:2011-06-13 07:10:15

标签: iphone objective-c uisearchbar

那里有任何善良的灵魂,

当我点击键盘上的任何值时,我一直收到此错误...我可以运行我的代码但是当我想要搜索时会出现此错误...

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM rangeOfString:options:]: unrecognized selector sent to instance 0x4e2a830'
*** Call stack at first throw:

请在这里帮助这个菜鸟..我在最后的智慧...... =(

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSDictionary *patients in listOfItems)
    {
         NSArray *array = [patients objectForKey:@"Patients"];
        [searchArray addObjectsFromArray:array];
    }

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length != 0)
            [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

2 个答案:

答案 0 :(得分:2)

搜索数组中的对象是 NSArray 对象,这意味着它们不会响应选择器 rangeOfString:,因为它是 NSString 方法

for (NSString *sTemp in searchArray) 
{ 
   NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
   if (titleResultsRange.length != 0)
     [copyListOfItems addObject:sTemp];
}

应该是这样的:

for(NSArray *array in searchArray)
{
   // NSString *str = [array objectAtIndex:0];
    PatientInfoObject *obj = [array objectAtIndex:0];
    NSString *str = obj.id;
    // to be sure
    if( [str isKindOfClass:[NSString class]] )
    {
       NSRange titleResultsRange = [str rangeOfString:searchText options:NSCaseInsensitiveCompare];
       if( titleResultsRange.length != 0 )
       {
         [copyListOfItems addObject:str];
       }
    }
    else
    {
        // this shouldn't have happened, log something to console
        NSLog(@"**Object in array is not of type NSString**");
     }
}

答案 1 :(得分:0)

您正在searchArray中添加数组对象。

你的应用程序在这里崩溃NSString *sTemp in searchArray因为sTemp包含NSArray对象而不是NSString对象。