gpt4 book ai didi

ios - 获取与 NSDictionary 对象类型相关的错误

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

我想根据国家名称或国家 ISD 代码过滤国家列表。例如,如果用户键入“9”,则应将印度添加到过滤数组中,因为印度的 ISD 代码为“+91”。我的数据源包含国家名称、ISD 代码等所有信息。

这是我的数组的结构:

- (void)viewDidLoad
{
[super viewDidLoad];

arrTotalCountries = [[NSMutableArray alloc] init];
arrFilteredCountries = [[NSArray alloc] init];

NSDictionary *dic1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"India",@"IN",@"+91",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"2",@"Australia",@"AU",@"+42",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"3",@"New Zealand",@"NZ",@"+63",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"4",@"South Africa",@"SA",@"+94",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic5 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"5",@"England",@"EN",@"+75",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic6 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"6",@"Israel",@"IS",@"+86",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic7 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"7",@"Afghanistan",@"AF",@"+27",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];
NSDictionary *dic8 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"8",@"Ireland",@"IR",@"+18",nil] forKeys:[NSArray arrayWithObjects:@"CountryId",@"CountryName",@"CountryCode",@"ISDCode",nil]];

[arrTotalCountries addObject:dic1];
[arrTotalCountries addObject:dic2];
[arrTotalCountries addObject:dic3];
[arrTotalCountries addObject:dic4];
[arrTotalCountries addObject:dic5];
[arrTotalCountries addObject:dic6];
[arrTotalCountries addObject:dic7];
[arrTotalCountries addObject:dic8];

[self filterContentForSearchText:@"9"];
}

这就是我尝试过滤数组的方式。出于测试目的,我刚刚通过了静态“9”。

- (void)filterContentForSearchText:(NSString *)searchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF.CountryName BEGINSWITH[cd] %@) OR (SELF.ISDCode BEGINSWITH[cd] %@)", searchText];
NSArray *filtered = [arrTotalCountries filteredArrayUsingPredicate:predicate];
NSLog(@"%@",[filtered description]);
}

我在运行时遇到以下错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (lhs = +91 rhs = ViewController)'

我认为这是因为,在字典中,键“ISDCode”有类似“+91”的值。我该如何解决这个问题?

最佳答案

你的谓词需要两个参数,但你只传递了1个,它应该是

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF.CountryName BEGINSWITH[cd] %@) OR (SELF.ISDCode BEGINSWITH[cd] %@)", searchText, searchText];

通过仅提供 1 个参数,第二个随机内存被占用,在本例中结果是一个 View Controller 。

关于ios - 获取与 NSDictionary 对象类型相关的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28736755/

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