NSPredicate导致App在UISearchBar中键入时崩溃

时间:2014-11-30 07:29:58

标签: ios objective-c arrays nspredicate

我相信我已将其缩小到此代码:

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

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    NSArray *array = [NSArray arrayWithArray:users];
    NSLog(@"%@",array);
    searchResults = [array filteredArrayUsingPredicate:resultPredicate];
    NSLog(@"SR: %@",searchResults);
    searchResults = array;
}

我从NSMutableArray users开始并将其“转换”为数组,创造性地命名为array。从这里我尝试用Predicate过滤数组。此时searchResults基于NSLog变得看似空洞。

这里发生了什么?为什么这会崩溃?如果我忽略Predicate函数并在数组中给searchResults两个对象,无论我输入什么,users中的前两个单元格都会出现。我尝试的其他所有内容都会让应用程序崩溃。

当应用程序崩溃时,我得到以下内容,非常有用(讽刺)控制台日志:

     *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x165025b0> valueForUndefinedKey:]: 
this class is not key value coding-compliant for the key name.'
    *** First throw call stack:
    (0x28fdbc1f 0x36786c8b 0x28fdb929 0x29cbe5ab 0x29c25677 0x29c618c9 0x29c6148b 0x29c60477 0x29c60299 0x355fd 0x3582f 0x2c677be5 0x2c677b1f 0x2c4cbc2b 0x2c4cbbd1 0x2c4b6863 0x2c677909 0x2c4d5ab5 0x2c64798b 0x2c646f29 0x2c677741 0x2c646d77 0x2c7df457 0x2c7df1ef 0x2c4c03cd 0x29cead6f 0x28fa2377 0x28fa1787 0x28f9fded 0x28eee211 0x28eee023 0x302e70a9 0x2c4fa1d1 0x21ac5 0x36d06aaf)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

消息非常清楚 - 您的数组包含NSString个对象,这些对象没有属性name,但您的谓词指定您要比较{{1}的值属性。

你可能想要

name