Akii_iOS Akii_iOS - 2 years ago 119
iOS Question

how to work with NSPredicate

I am try to search word which contain "EVBAR02". Can any body help me in this case. Here is Array Directory.

Here is code.

`NSMutableArray *filteredListContent = [NSMutableArray arrayWithArray:arrayGallery];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"imageUrl CONTAINS[cd] EVBAR02"];
[filteredListContent filterUsingPredicate:predicate];`

arrayGallery = (
imageId = "04";
imageUrl = "/article/THEV-EVBAR04.jpg";
imageId = "02";
imageUrl = "/article/THEV-EVBAR02.jpg";
imageId = "06";
imageUrl = "/article/THEV-EVBAR06.jpg";

But Its not working. What to do?

Answer Source

The ...WithFormat part of predicateWithFormat: works the same way like stringWithFormat:

NSString *searchString = @"EVBAR02";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageUrl CONTAINS[cd] %@", searchString];

Or literally in single quotes:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageUrl CONTAINS[cd] 'EVBAR02'"];

The quotation is required as described in the documentation:

String constants must be quoted within the expression—single and double quotes are both acceptable, but must be paired appropriately (that is, a double quote (") does not match a single quote ('))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download