Ios 内部带有NSObject的筛选器NSArray
我想在我的tableView中使用搜索栏。 我与Person(NSObject)有协议。在cellForRow中,我使用以下代码:Ios 内部带有NSObject的筛选器NSArray,ios,objective-c,nsarray,uisearchbar,nspredicate,Ios,Objective C,Nsarray,Uisearchbar,Nspredicate,我想在我的tableView中使用搜索栏。 我与Person(NSObject)有协议。在cellForRow中,我使用以下代码: Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; cell.textLabel.text = person.fullName; 对于搜索栏,此代码为: - (void)searchForText:(NS
Person *person = [[self.sectionedPersonName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
对于搜索栏,此代码为:
- (void)searchForText:(NSString *)searchText {
if (searchText.length > 0) {
searchBar = YES;
} else {
searchBar = NO;
}
NSLog(@"%@", self.sectionedPersonName);
NSPredicate *predicate; = [NSPredicate predicateWithFormat:@"SELF.fullName beginswith[c] %@", searchText];
self.searchResults = [self.sectionedPersonName filteredArrayUsingPredicate:predicate];
}
self.sectionedPersonName
包含以下内容:
(
(
"<Person: 0x7fc0f6012650>"
),
(
),
(
),
(
"<Person: 0x7fc0f600f8a0>",
"<Person: 0x7fc0f60132e0>"
),
(
),
(
),
(
),
(
"<Person: 0x7fc0f6012d80>"
),
(
),
(
"<Person: 0x7fc0f6012b30>"
),
(
"<Person: 0x7fc0f6010100>"
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
),
(
)
)
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (lhs = (
"Anna Haro"
) rhs = )'
你没有一系列的人,你有一系列的人。因此,您不能简单地过滤数组,因为您的所有比较实际上都是尝试将字符串数组与字符串进行比较。这就是你看到的“奇怪”结果和崩溃的原因
最好的选择是创建自己的循环来迭代外部数组,然后在每个内部数组上运行谓词。将过滤后的数组添加到一个新的可变数组中,该数组保存整个结果。您没有人员数组,您有人员数组。因此,您不能简单地过滤数组,因为您的所有比较实际上都是尝试将字符串数组与字符串进行比较。这就是你看到的“奇怪”结果和崩溃的原因
最好的选择是创建自己的循环来迭代外部数组,然后在每个内部数组上运行谓词。将得到的过滤数组添加到一个新的可变数组中,该数组保存整个结果。谢谢!我将尝试在上面循环。:)谢谢你,韦恩!我将尝试在上面循环。:)
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (lhs = (
"Anna Haro"
) rhs = )'