获取与 NSDictionary 对象类型相关的错误
Getting error related to NSDictionary object type
我想根据国家名称或国家 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 个参数,第二次使用随机内存,在这种情况下变成了视图控制器。
我想根据国家名称或国家 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 个参数,第二次使用随机内存,在这种情况下变成了视图控制器。