Reputation: 53
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?
Upvotes: 1
Views: 151
Reputation: 285079
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 ('))
Upvotes: 1
Reputation: 5435
There is no need of NSString stringWithFormat
, directly use NSPredicate predicateWithFormat
as below:
NSMutableArray *filteredListContent = [NSMutableArray arrayWithArray:arrayGallery];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageUrl CONTAINS[cd] 'EVBAR02'"];
[filteredListContent filterUsingPredicate:predicate];
Hope it will help:)
Upvotes: 0
Reputation: 1549
NSString *myString = @"EVBAR02";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageUrl
contains[cd] %@", myString];
NSMutableArray *arrResult =[filteredListContent filteredArrayUsingPredicate:predicate];
Upvotes: 0
Reputation: 1235
You have to set single quotes in your search string :
NSPredicate *predicate = [NSPredicate predicateWithFormat : @"imageUrl CONTAINS[cd] 'EVBAR02' "]
Upvotes: 0
Reputation: 3955
Vadian is right.
And if you want to pass the field name as a parameter, use %K
in place of %@
:
NSString *searchField = @"imageUrl";
NSString *searchString = @"EVBAR02";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS[cd] %@", searchField, searchString];
Upvotes: 0