Search code examples
iossearchnspredicate

How to search data from multiple keys of dictionary?


In my application having search functionality and I want to search value from multiple keys. This is my dictionary:

 notifications =     (
                {
            "beautician_name" = "ABC";
            "booking_date" = "2016-12-12";
            "date_time" = "2016-12-12 11:27:42";
            discount = 25%;
            "service_name" = Tanning;
            "sub_service_name" = "Spray Tan";
            title = Cancellation;
        },
        {
            "beautician_name" = "PQR";
            "booking_date" = "2016-12-11";
            "date_time" = "2016-12-12 11:27:42";
            discount = 25%;
            "service_name" = hair;
            "sub_service_name" = "Spray Tan";
            title = Cancellation;
        },
        {   
            "beautician_name" = "XYZ";
            "booking_date" = "2016-12-15";
            "date_time" = "2016-12-12 11:27:42";
            discount = 25%;
            "service_name" = film;
            "sub_service_name" = "Spray Tan";
            title = Cancellation;
        }
)

I want to search data from these keys: "beautician_name", "booking_date", "discount", "sub_service_name". I want to predicate not any for loop or etc.


Solution

  • I have not tested it, please try something like this below.

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"beautician_name contains[c] %@ OR booking_date contains[c] %@ OR discount contains[c] %@ OR sub_service_name contains[c] %@" ,searchText,searchText,searchText,searchText];
            NSArray *SearchResult = [notifications filteredArrayUsingPredicate:resultPredicate];