HML
HML

Reputation: 103

Predicate in array

I have an array which contains lots of info like lat,lon,city name etc.But all this info stored by class object.

("<GetCityList: 0x1fd334d0>",
"<GetCityList: 0x1fd33560>",
"<GetCityList: 0x1fd33310>",)

my array is looking like this.if i want city name then first i create GetCityList object and then object.cityName. Now i have to search lat and lon by comparing cityName.so how can i do,because it consumes too much time?can i use predicate ?how?

Upvotes: 0

Views: 132

Answers (3)

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

Use it in this way also:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cityName contains %@", @"cityNamehere"]

NSMutableArray *filtered = [cityListArray filterUsingPredicate:predicate];

Upvotes: 0

Janak Nirmal
Janak Nirmal

Reputation: 22726

Let say you have an array of cityList and want to Filter this by city name you can do

NSPredicate *cityNamePredicate = [NSPredicate predicateWithFormat:@"cityName contains[c] %@", @"cityName"]
NSArray *filteredCityList = [cityList filteredArrayUsingPredicate:cityNamePredicate];

Above filteredCityList will be your required filtered array whatever you passed in parameter. Here [cd] means case and diatric insensitive

You can refer this tutorial for further exploring NSPredicate.

Upvotes: 3

Tatarasanu Victor
Tatarasanu Victor

Reputation: 654

Example:

self.data = [NSArray arrayWithObjects:
                         [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"To Airport",@"TO/AIR", nil] forKeys:[NSArray arrayWithObjects:kName,kCode, nil]],
                         [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Point-to-Point",@"PTP", nil] forKeys:[NSArray arrayWithObjects:kName,kCode, nil]],[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"From Airport",@"FR/AIR", nil] forKeys:[NSArray arrayWithObjects:kName,kCode, nil]], nil];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name CONTAINS[cd]  %@)",searchText];
self.searchableData = [self.data filteredArrayUsingPredicate:predicate];

Based on this you can replace name with cityName.

Upvotes: 0

Related Questions