gpt4 book ai didi

ios - 使用谓词搜索字符串时提取数组的行号

转载 作者:行者123 更新时间:2023-11-29 02:10:19 25 4
gpt4 key购买 nike

我不确定如何提出问题,所以如果我说出问题是什么,也许有人可以提供帮助。

我有一个 SQL lite 表,我用它来填充两个数组:一个是姓名和姓氏,另一个是同时填充数据库中人员 ID 的数组。

然后我在 TableView 中显示这些名称。我使用 Predicate 方法在原始数组(称为 storiesArray)中搜索某人的名字。

但是,当我选择要显示的这个人时,它会显示故事数组中的第一个人。

我已经尝试获取找到搜索字符串的行,但这会返回一个非常大的数字,即使它确实找到了字符串(使用 Predicates 解释)。

如何获取找到的字符串的行号?

所以,我尝试了以下代码:

  NSUInteger indexOfTheObject = [storiesArray indexOfObject: searchText];

但这会返回数字 2147483647,实际上是 -1。

我知道 searchText 在 stories 数组中,因为下面的代码确实返回了一个有效的搜索结果:

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

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];

self.searchResult = [NSMutableArray arrayWithArray:[storiesArray filteredArrayUsingPredicate:resultPredicate]];
}

self.searchResult 会填充正确的字符串。

提前致谢。

最佳答案

这里有类似的主题:

How retrieve an index of an NSArray using a NSPredicate?

how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsingPredicate as filter

http://useyourloaf.com/blog/2010/10/19/searching-arrays-with-nspredicate-and-blocks.html

NSUInteger index = [self.myarray indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop) {
return [predExists evaluateWithObject:obj];
}];

关于ios - 使用谓词搜索字符串时提取数组的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com