NSPratik
NSPratik

Reputation: 4848

Getting error related to NSDictionary object type

I want to filter list of countries based on either Country name or Country ISD code. For example, if user types '9', India should be added to filtered array because ISD code of India is "+91". My datasource contains all the information like country name, ISD code etc..

Here is how my Array is structured:

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

And here is how I am trying to filter array. I have just passed static '9' for testing purpose.

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

I am getting following error while run.

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)'

I think this is because, there is value like '+91' for key "ISDCode", in the dictionary. How can I resolve this issue?

Upvotes: 0

Views: 158

Answers (1)

Wain
Wain

Reputation: 119031

Your predicate requires two parameters but you only pass 1, it should be

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

By only supplying 1 parameter random memory was taken for the second and in this case turned out to be a view controller.

Upvotes: 1

Related Questions