使用字符串搜索 NSMutableArray 并返回整个比较数组

发布于 2024-09-27 10:09:37 字数 2531 浏览 8 评论 0原文

看来我的问题对任何人来说都不是问题,因为我还没有找到任何有关它的信息。 所以这可能不是一个大问题,但对我来说却是。

我有这个 MutableArray,其中填充了 XML 文件中的大量数据。 -姓名 -年龄 -地址 搜索是针对“名称”的,到目前为止,过滤工作得很好。

我所做的是使用 rangeOfString 搜索数组,但只返回字符串(-Name),而不是像原始数组那样返回其内容的数组,因为它现在只是一个字符串。

谁能告诉我如何完成这个

这是我到目前为止的搜索

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

搜索器是搜索栏中的字符串。 或者是否有其他更有效的方法或者是否可以在 MutipleArry 中搜索任何值?!?

欢迎任何想法和建议

我将代码更改为此

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSDictionary *dictionary in listOfContent)
{
    //NSArray *array = [dictionary objectForKey:LNAME];
    [searchArray addObject:dictionary];

}

for (NSString *sTemp in searchArray)
{
    NSLog(@"array %@", searchArray);

    if ([sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)

        [filteredListOfContent addObject:searchArray];
}

日志显示过滤器似乎可以工作但后来我收到此错误

2010-10-22 16:18:09.708 TableView[6114:207] -[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540
2010-10-22 16:18:09.712 TableView[6114:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540'

任何人都可以告诉我问题是什么

仍然没有找到解决方案我将代码更改为:

NSMutableArray *searchArray = [[NSMutableArray alloc] init];

        for (NSDictionary *dictionary in contentsList)
        {
            NSArray *array = [dictionary allValues];
            [searchArray addObjectsFromArray:array];
        }
        for (NSDictionary *dict in searchArray)
        {
            if ([[dict valueForKey:@"NAME"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) {
                NSLog(@"Filter %@", dict);
                [searchResults addObject:dict];
            }

现在我有具有值的数组但仍然出现错误

2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'

任何人都可以解释一下该错误意味着什么还是我做错了什么?!?

it seems my Problem isn't a problem for anybody eles because I havend fount anything about it.
so it maybe isn't such a big Problem but for me it is.

I have this MutableArray filled with alot of data from an XML file.
-Name -Age -Address
The search goes for the Name, and the filtering works pretty fine so far.

what I do is search the array with rangeOfString but that only returns the String (-Name) and not the Array with it's content like the original Array because its only a string now.

can anyone tell me how do I accomplish this

That's my search so far

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

searcher is the String in the SearchBar.
or is there any other more efficent way or is it possible to search for any value in the MutipleArry?!?

Any Ideas and suggestions are welcome

I changed the code to this

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSDictionary *dictionary in listOfContent)
{
    //NSArray *array = [dictionary objectForKey:LNAME];
    [searchArray addObject:dictionary];

}

for (NSString *sTemp in searchArray)
{
    NSLog(@"array %@", searchArray);

    if ([sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)

        [filteredListOfContent addObject:searchArray];
}

the log shows that the filter seems to work but then I get this error

2010-10-22 16:18:09.708 TableView[6114:207] -[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540
2010-10-22 16:18:09.712 TableView[6114:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x5c3f540'

can anyone tell me what the problem is

And Still no solution found I changed the Code to this:

NSMutableArray *searchArray = [[NSMutableArray alloc] init];

        for (NSDictionary *dictionary in contentsList)
        {
            NSArray *array = [dictionary allValues];
            [searchArray addObjectsFromArray:array];
        }
        for (NSDictionary *dict in searchArray)
        {
            if ([[dict valueForKey:@"NAME"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) {
                NSLog(@"Filter %@", dict);
                [searchResults addObject:dict];
            }

now i Have the array with the values but still get the error

2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'

can anyone explain me waht taht that error means or waht I did wrong?!?

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

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

发布评论

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

评论(1

泪冰清 2024-10-04 10:09:37
2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'

该错误意味着您通过向 -objectForKey: 发送消息 -objectForKey: 来将 NSString 视为 NSDictionary

2010-10-28 16:23:46.124 TableViews[8373:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5a5eb00'

That error means you treated an NSString as if it were an NSDictionary by sending the message -objectForKey: to it.

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