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

时间:2015-02-26 07:32:31

标签: ios objective-c nsarray nsdictionary nspredicate

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

以下是我的Array的结构:

- (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]);
}

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

  

由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:'无法使用不是字符串的字符串执行子字符串操作(lhs = +91 rhs = ViewController )'

我认为这是因为,有' + 91'对于密钥" ISDCode",在字典中。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

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

仅提供1个参数随机存储器用于第二个,在这种情况下结果是视图控制器。