iOS NSComparisonresult问题

时间:2011-11-06 11:20:01

标签: objective-c ios comparison

当我在搜索字段中输入两个以上的字符时会引发异常。我使用NSComparisonResult在UITableView上显示结果:

- (void)filterContentForSearchText:(NSString*)searchText
{

for (mystring in self.array)
{

NSComparisonResult result = [mystring compare:searchText options:(NSCaseInsensitiveSearch) 
range:NSMakeRange(0, [searchText length])];

if (result == NSOrderedSame)
        {   
            NSUInteger index=[self.array indexOfObjectIdenticalTo:mystring]; 
            NSUInteger maxindex = index + 50;
            for (index ; (index < [self.array count] && index <= maxindex && index!= NSNotFound); index ++)
            {   
                [self.filteredListContent addObject:[NSDictionary dictionaryWithObjectsAndKeys:[self.array objectAtIndex:index],@"english",[self.secondarray objectAtIndex:index],@"translated",nil]];  
            }
            break;
        }
}

输出显示:

 -[UIDeviceWhiteColor compare:options:range:]: unrecognized selector sent to instance 0x5e4a5d0
2011-11-06 12:10:51.932 XXX[2583:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceWhiteColor compare:options:range:]: unrecognized selector sent to instance 0x5e4a5d0'

在这种情况下,UIDeviceWhiteColor是什么意思?


self.array = [[NSArray alloc] initWithArray:
           [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"first" ofType:@"txt"]
                                      encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];

self.secondarray = [[NSArray alloc] initWithArray:
           [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"second" ofType:@"txt"]
                                      encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:@"\n"]];

2 个答案:

答案 0 :(得分:1)

那是因为UIDeviceWhiteColor中某处self.array,应该仅由NSString组成。你如何填充这个数组?

答案 1 :(得分:1)

UIDeviceWhiteColor中有self.array个对象。您还应该将mystring转换为NSString或任何类型的对象。如果您将其投放到UIDeviceWhiteColor,则编译器会向您发出警告,UIDeviceWhiteColor 回复compare:options:range:

for (NSString *mystring in self.array)
{
    //some code
}