0
votes

What 's wrong with predicate format below ?

    if (devices && devices.count > 0 && ![tf_Search.text isEqualToString:@""]) {
    NSString* strSearch = [NSString stringWithFormat:@"%@", tf_Search.text];
    NSPredicate* preFilter =
        [NSPredicate predicateWithFormat:@"(dRIV_NAMESURNAME CONTAINS[cd] %1$@) || (vEHI_PLATE CONTAINS[cd] %1$@)",
                     strSearch];
    devicesFitered = [devices filteredArrayUsingPredicate:preFilter];
}

devicesFiltered array s not null and has 4 coredata device entity.

dRIV_NAMESURNAME and vEHI_PLATE are entity fields. NSString.

it gives > "'NSInvalidArgumentException', reason: 'Unable to parse the format string" error

if i use them seperatelly like in below 2 formats, no problem

@"(dRIV_NAMESURNAME CONTAINS[cd] %@)" > no problem

or

@"(vEHI_PLATE CONTAINS[cd] %@)" > no problem

is it about %1$@ ?

is it not possible in objc to address inputs like in cSharp "{1} asdas da {1} sdfs {2}"

What is the correct format ?

1

1 Answers

1
votes

No %1$@ is not permitted. The easiest solution is just to repeat the strSearch argument:

    NSPredicate* preFilter = [NSPredicate predicateWithFormat:@"(dRIV_NAMESURNAME CONTAINS[cd] %@) || (vEHI_PLATE CONTAINS[cd] %@)", strSearch, strSearch];

If you have a very long predicate with many duplicated arguments, you could also consider using NSPredicate's predicateWithSubstitutionVariables method. See the Apple docs.