I have CoreData Table with NSString field. But in the filter I need to be interpreted as float.
fieldName.floatValue does not give the correct result. If any way to do this without changing the field type in the table ?
Edit:
Data is:
company = "CompanyName";
date = "2016-01-17";
value = "379.76";
My predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"value.floatValue > 2000"];
But value 379.76 enters the result. Using value.floatValue all the same values are compared as strings.
TNX
If you know value is not negative, you can use this trick:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"abs:(value) > 2000"];
The abs: forces SQLite to treat the value attribute as a number.
Alternatively, add zero to it (which works for negative numbers):
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"add:to:(value,0) > 2000"];
I think you should change String type to Number, this is the easiest way.
You can use this predicate:
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [[evaluatedObject value] floatValue] > 2000; // Change id to your Class
}];
But this predicate can not be used with CoreData. So you should store the fetch result to an array, then use this predicate with it.
//Try with this.
// '>' will apply when 'value' is number. So, I first put your values in an array. Only then, I can apply the predicate.
NSString *company = #"CompanyName";
NSString *date = #"2016-01-17";
NSString *value = #"379.76";
NSArray *dataArray = #[#(value.floatValue),#(379.76),#(379),#(200)];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"self > %f",200.0];
NSArray *array = [dataArray filteredArrayUsingPredicate:predicate];
//output:#(379.76),#(379.76),#(379)
Try this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"value > 2000"];
Related
Is it possible to use nspredicate to compare whether one NSArray is exactly equal to another NSArray of strings? I need this dome via predicates because of its possible I will add this predicate to a compound predicate.
The Array I am comparing against is a property of an NSDictionary.
So the answer was a mixture of both, I did use the predicatewithformat but got creative in the string inside, inspired by #akashivskyy and #avi
[predicatesArray addObject:[NSPredicate predicateWithFormat:#"ANY dynamic.values == %#", arr]];
Edit: As (partially) suggested by Avi, you may use the equality predicate:
NSArray *otherArray = #[ #"foo", #"bar" ];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"self == %#", otherArray];
[predicate evaluateWithObject:#[ #"foo", #"bar" ]]; // YES
[predicate evaluateWithObject:#[ #"baz", #"qux" ]]; // NO
Alternatively, and if you have any trouble with format string in the future, you may always use a block predicate to perform your own logic:
NSArray *otherArray = #[ #"foo", #"bar" ];
NSPredicate *predicate = [NSPredicate predicateWithBlock:^(NSArray *evaluatedArray, NSDictionary<NSString *, id> *bindings) {
return [evaluatedArray isEqualToArray:otherArray];
}];
// use the predicate
I have to get a particular string from a list of strings using NSPredicate (CoreData). For eg:
if I have the following array from CoreData
Helloworld
helloworld1234
helloworld 12345
from the above list I need only 1 and 3 one as result.
I have tried the following but i get all the three as result
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"contactName CONTAINS [c]%#",[arySeperator objectAtIndex:1]];
NSArray *filteredArray1 = [arrayContacts filteredArrayUsingPredicate:predicate];
Where I am going wrong?
To find all entries that contain the given text as a "whole word", you can use the
predicate "MATCHES" operator, which matches against a regular expression, and the
word boundary pattern \b:
NSString *searchWord = #"Helloworld";
NSString *pattern = [NSString stringWithFormat:#".*\\b%#\\b.*", searchWord];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"contactName MATCHES[c] %#", pattern];
This should also work if you add this predicate to the Core Data fetch request,
instead of filtering an already fetched array of managed objects.
LIKE
The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.
NSString *textSearch = #"Raja";
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"firstname LIKE '*%1$#*' OR lastname LIKE '*%1$#*'", textSearch];
You can use below code to get if a string is present in an NSArray:
NSArray* yourArray = [NSArray arrayWithObjects: #"Str1", #"Str2", #"Str3", nil];
if ( [yourArray containsObject: yourStringToFind] ) {
// do found
} else {
// do not found
}
I would like to filter an NSArray of NSDictionaries, however I would like to filter the result using one, two or even three NSPredicate values?
Currently I am filtering my array by doing this.
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"parts == %#", filterString];//keySelected is NSString itself
NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[currentParts filteredArrayUsingPredicate:predicateString]];
sortedItemsArray = [filteredArray mutableCopy];
But I am not sure how I would do this using two predicates?
The other two predicates individually look like the one above accept different keys.
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"area == %#", filterString];
and
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"item == %#", filterString];
What I was thinking is that maybe you could have something like
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"stage == %# area == %#", filterString, areaflterstring];
But I don't think that's possible.
It is possible, but you need to tell the predicate how to combine the parts, like:
NSPredicate *predicateString = [NSPredicate predicateWithFormat:#"stage == %# AND area == %#", filterString, areaflterstring];
You can alternatively use NSCompoundPredicate to combine a number of predicates.
I have a plist working with a search display controller which contains an array of dictionaries with some data members like.
<root> (array)
<"Item 0"> (dictionary)
<"Name"></"Name" (String)
<"Work"></"Work"> (String)
<"Age"></"Work"> (Number)
</"Item 0">
<"Item 1">
....
</"Item 1">
</root>
I would like to use an NSPredicate to filter all the names that match with the search criteria. For example searching "an" for all names will yield "Sandy" and "Alexander."
So far I've tried things like:
NSPredicate *p = [NSPredicate predicateWithFormat:#"Name == %#",
filterText];
Results = [data filteredArrayUsingPredicate:p];
Any ideas? Thanks.
Use CONTAINS in NSPredicate
Below code gives case sensitive search
NSPredicate *p = [NSPredicate predicateWithFormat:#"Name CONTAINS %#",
filterText];
Results = [yourPlistDataArray filteredArrayUsingPredicate:p];
Below code gives case insensitive search
NSPredicate *p = [NSPredicate predicateWithFormat:#"ANY Name CONTAINS %#",
filterText];
Results = [yourPlistDataArray filteredArrayUsingPredicate:p];
EDIT : Refer more here
this can show you the very generic type of the searching in any NSArray. it is adjusted to your NSDictionary items currently, but you can create even more complex conditions here to filter it.
NSArray *originalArray = ...;
NSString *searchText = ...; // this is a parameter only, it can be set freely
NSArray *_filteredArray = [originalArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDictionary *_dataRow = (NSDictionary *)evaluatedObject;
return ([[[_dataRow valueForKey:#"Name"] lowercaseString] rangeOfString:[searchText lowercaseString]].location != NSNotFound);
}]];
NSLog (#"%#", _filteredArray);
I have a array that contains objects of a custom class, and I would like to filter the array based on if one of the classes attributes contains a custom string. I have a method that is passed the attribute that I want to be searched (column) and the string that it will search for (searchString). Here is the code I have:
NSPredicate *query = [NSPredicate predicateWithFormat:#"%K contains %K", column, searchString];
NSMutableArray *temp = [displayProviders mutableCopy];
[displayProviders release];
displayProviders = [[temp filteredArrayUsingPredicate:query] mutableCopy];
[temp release];
However, it always throws an exception on
displayProviders = [[temp filteredArrayUsingPredicate:query] mutableCopy];
saying this class is not key value coding-compliant for the key [whatever searchString is].
Any ideas what I am doing wrong?
[NSPredicate predicateWithFormat:#"%# contains %#", column, searchString];
When you use the %# substitution in a predicate format string, the resulting expression will be a constant value. It sounds like you don't want a constant value; rather, you want the name of an attribute to be interpreted as a key path.
In other words, if you're doing this:
NSString *column = #"name";
NSString *searchString = #"Dave";
NSPredicate *p = [NSPredicate predicateWithFormat:#"%# contains %#", column, searchString];
That is going to be the equivalent to:
p = [NSPredicate predicateWithFormat:#"'name' contains 'Dave'"];
Which is the same as:
BOOL contains = [#"name rangeOfString:#"Dave"].location != NSNotFound;
// "contains" will ALWAYS be false
// since the string "name" does not contain "Dave"
That's clearly not what you want. You want the equivalent of this:
p = [NSPredicate predicateWithFormat:#"name contains 'Dave'"];
In order to get this, you can't use %# as the format specifier. You have to use %K instead. %K is a specifier unique to predicate format strings, and it means that the substituted string should be interpreted as a key path (i.e., name of a property), and not as a literal string.
So your code should instead be:
NSPredicate *query = [NSPredicate predicateWithFormat:#"%K contains %#", column, searchString];
Using #"%K contains %K" doesn't work either, because that's the same as:
[NSPredicate predicateWithFormat:#"name contains Dave"]
Which is the same as:
BOOL contains = [[object name] rangeOfString:[object Dave]].location != NSNotFound;
Replace %K to %# in your predicate string,
#"%# contains %#", column, searchString
This Works for me
[self.array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"name contains 'Dave'"]];