搜索栏控制器 - 搜索结果时崩溃

发布于 2024-12-07 08:03:34 字数 835 浏览 1 评论 0原文

我正在实现一个搜索栏控制器来搜索表视图。下面执行搜索的方法代码崩溃,并出现错误“-[__NSArrayM rangeOfString:options:]: 无法识别的选择器发送到实例 0x65558e0'

locationInfo 数组是一个包含 26 个数组的数组,每个数组都包含由多个对象组成的数组 吗

任何人都可以建议为什么代码崩溃

- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];

if ([self searchResults] == nil)
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setSearchResults:array];
    [array release], array = nil;
}

[[self searchResults] removeAllObjects];

if ([[self savedSearchTerm] length] != 0)
{
    for (NSString *currentString in [self locationInfo])
    {
        if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            [[self searchResults] addObject:currentString];
        }
    }
}
}

I am implementing a search bar controller to search a table view. The below method code which performs the search is crashing with the error "-[__NSArrayM rangeOfString:options:]: unrecognized selector sent to instance 0x65558e0'

The locationInfo array is an array containing 26 arrays, each of which contains a number of objects made up of strings.

Can anyone suggest why the code is crashing ?

Thank you.

- (void)handleSearchForTerm:(NSString *)searchTerm
{
[self setSavedSearchTerm:searchTerm];

if ([self searchResults] == nil)
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setSearchResults:array];
    [array release], array = nil;
}

[[self searchResults] removeAllObjects];

if ([[self savedSearchTerm] length] != 0)
{
    for (NSString *currentString in [self locationInfo])
    {
        if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            [[self searchResults] addObject:currentString];
        }
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赏烟花じ飞满天 2024-12-14 08:03:34

正如您在问题中所说,“locationInfo”是包含 26 个数组的数组,
所以,
[self locationInfo] 中的 currentString 将仅返回一个数组,因此尝试编写如下所示的内容:

for (NSArray *currentArray in [self locationInfo])

{
for (NSString *currentString in currentArray)
{
    if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location !=  NSNotFound)
    {
        [[self searchResults] addObject:currentString];
    }
}

}

或类似的内容

As you stated in question that "locationInfo" is array containing 26 arrays,
so,
currentString in [self locationInfo] will return an array only so try to write something like below :

for (NSArray *currentArray in [self locationInfo])

{
for (NSString *currentString in currentArray)
{
    if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location !=  NSNotFound)
    {
        [[self searchResults] addObject:currentString];
    }
}

}

or something like this

撑一把青伞 2024-12-14 08:03:34

根据您收到的错误,似乎 [self locationInfo] 返回一个数组 (NSArray) 而不是字符串 (NSString)你在期待。

Based on the error you're getting, it seems [self locationInfo] returns an array (NSArray) and not a string (NSString) as you're expecting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文