gpt4 book ai didi

ios - 应用程序在搜索(过滤)NSDictionary 时崩溃

转载 作者:行者123 更新时间:2023-11-29 12:09:21 25 4
gpt4 key购买 nike

所以我有以下代码:

我的 .h 文件

    @interface TableViewController : UITableViewController <UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
{
IBOutlet UITableView *myTableView;
IBOutlet UISearchBar *mysearchBar;
NSMutableArray *filteredList;
BOOL isFiltered;
}
@property NSDictionary *iconSet;

@end

以及 .m 文件中出错的部分。

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

if(searchText.length == 0)
{
//bool in .h file
isFiltered = NO;
}else
{
isFiltered = YES;
filteredList = [[NSMutableArray alloc] init];

//self.iconSet is an NSDictionary (coming from a segue) formed like -(NSDictionary *) symbols { return self.symbols = @{@"💘":@"Heart With Arrow",@"❤️":@"Heavy Black Heart"}
for (NSDictionary *theDictionary in self.iconSet) {

for (NSString *key in theDictionary) {
NSString *value = [theDictionary objectForKey:key];
NSRange stringRange = [value rangeOfString:searchText options:NSCaseInsensitiveSearch];

if (stringRange.location != NSNotFound) {
[filteredList addObject:theDictionary];
break;
}
}
}
}
[myTableView reloadData];
}

xCode 没有向我发出任何错误或警告,但当我按下搜索栏中的字母或图标时,应用程序崩溃,并显示以下消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1000f94d0'

我尝试在 stackoverflow 上查找它,但没有找到我要找的答案。我希望这里有人可以帮助我找到我的(可能是愚蠢的)错误。

最佳答案

因为 self.iconSet 是一个字典,用 for..in 迭代它会给你字符串形式的键。因此,不需要第二个循环:

for (NSString *key in self.iconSet) {
NSString *value = [self.iconSet objectForKey:key];
...
}

或更好:

[self.iconSet enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
...
}]

关于ios - 应用程序在搜索(过滤)NSDictionary 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33955942/

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