SGDev
SGDev

Reputation: 2252

how to create a predicate in which % work as a string in ios

I am using a Predicate for filtering a NSArray but, when a create Predicate like this

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains[c] %@ && %K contains[c] %@ ",
                                                            kUnitUnitcategoryKey,
                                                            @"Blood Pressure Diastolic",
                                                            kUnitUnitNameKey,
                                                            [_txtOxygenSaturationUnit.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

after this "predicate" string is : "unit_category CONTAINS[c] "Blood Pressure Diastolic" AND unit_name CONTAINS[c] "%" "

NSArray *results = [_arrUnitsName filteredArrayUsingPredicate:predicate];

it will return Zero records.

Now, how can i used % as a string.

Upvotes: 0

Views: 202

Answers (1)

Mark McCorkle
Mark McCorkle

Reputation: 9414

This is an extremely ugly way to write a somewhat complex predicate. You'll find using NSCompoundPredicates are MUCH easier to manage and modify. Simply create your multiple NSPredicates then use an andPredicate to combine all of your predicates.

What is the best way to build a complex NSCompoundPredicate?

As far as the actual question of how can you use % as a string? Test each predicate individually if you are getting zero results for easier debugging and you'll find the issue much easier.

Upvotes: 1

Related Questions