issue in NSPredicate for NSString - ios

In my application I am using CoreData.Amount values are stored in coreData as NSString. In my Interface Builder i have two textfields.When i enter any amount on my textfields This amounts will accept as Minimum Value and maximum Value.I want to get all amounts between minimum and maximum amounts i have entered.
What will be the solution.I just goes through this way Example It Not be worked on my project because i want to convert string to integer on my NSPredicate method.
My code is
NSPredicate *p1= [NSPredicate predicateWithFormat:#"(amount.intValue => %#) && (amount.intValue <=%#)", text1.intValue,text2.intValue]];
Note that amount is stored as NSString in coredata

NSPredicate *p1= [NSPredicate predicateWithFormat:#"(amount.intValue => %#) && (amount.intValue <=%#)", text1.intValue,text2.intValue]];
You shouldn't be comparing numbers and strings. Compare one or the other. In this case, you want to compare numbers. You should change your source data stored in the model to be a number.
=> should be >=
%d is an integer parameter format, and text1.intValue returns an integer. Using %# expects an on jest and won't do what you want.
Log the contents of the predicate so you can see what it contains. But mainly, change the type of the data in the model.

As you store amount as NSString, you need to pass it as NSNumber when doing the evaluation.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.intValue <= $max.intValue AND SELF.intValue >= $min.intValue"];
Now you can evaluate the amount to see if it is in range
if ([predicate evaluateWithObject:[NSNumber numberWithInt:amount.intValue] substitutionVariables:#{#"max":[NSNumber numberWithInt:text1.intValue], #"min":[NSNumber numberWithInt:text2.intValue]}])
{
//Got the correct amount
}

Currect Answer is
NSPredicate *p1= [NSPredicate predicateWithFormat:#"(amount >= %d) && (amount <= %d)", text1.intValue, text2.intValue]];

Related

IOS/Objective-C: NSPredicate and NULL values in core data fetch

I am trying to fetch items that do not have a value of 1 in an NSNumber attribute. Some of the items I want to fetch have never had the attribute set at all and therefore have a NSNull value. How can I fetch items that are not 1 but may be NSNull?
The attribute is an NSNumber and I am also a bit confused about whether I should be searching on 1 or #1.
The following code is excluding NSNull values when I want to include them:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"&hasserverid!=1"];
Thanks for any suggestions.
You can explicitly test for both cases with an OR:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"hasserverid != YES OR hasserverid == NULL"];
By the name of your property, I assume when you say 1 you're actually referring to the value true. Using YES in the predicate format instead of 1 makes your predicate more legible.

How to set predicate for a string attribute holding integer value in core data

How to set predicate for a string attribute holding integer value in core data to get the in-between values in integer.
I am using compound predicate for multiple conditions and a piece of code follows..
NSPredicate *categoryPredicate = [NSPredicate predicateWithFormat: #"rating >= %# AND rating <= %#",ratingFrom.text,ratingTo.text];
[compoundPredicateArray addObject: categoryPredicate ];
"rating" attribute is of type string in database. I need to get the rating between the given range in the ratingFrom & ratingTo textfields.
"1" to "10" should fetch all ratings in between 1 to 10.how can I achieve this, without changing the attribute type to number.
Any help appreciated..Thanks
i am not confirm but try this
convert your values in NSString and put this code
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"string CONTAINS[c] %#",searchText];
Ideally you should change this attribute to be a number rather than a string. But if that's not possible, the following should force CoreData (or rather SQLite) to treat it as a number:
NSPredicate *categoryPredicate = [NSPredicate predicateWithFormat: #"add:to:(rating,0) >= %d AND add:to:(rating,0) <= %d",ratingFrom.text.intValue,ratingTo.text.intValue];
you can try BETWEEN expression:
BETWEEN The left-hand expression is between, or equal to either of,
the values specified in the right-hand side. The right-hand side is a
two value array (an array is required to specify order) giving upper
and lower bounds. For example, 1 BETWEEN { 0 , 33 }, or $INPUT BETWEEN
{ $LOWER, $UPPER }. In Objective-C, you could create a BETWEEN
predicate as shown in the following example:
So your code should look like this:
NSPredicate *betweenPredicate =
[NSPredicate predicateWithFormat: #"rating BETWEEN %#", #[ratingFrom, ratingTo]];
ratingFrom, ratingTo should be NSNumber instances if rating is NSNumber.

Extract objects with specific date from custom object Array

I have an ordered array of Availability object which is a class containing two attributes, startAt and endAt.
I'd like to extract from my availability array the objects from a specific day without taking into account the time.
I'd also like to use NSPredicate instead of "for" loop to do it.
Could you help me?
Thanks
for-in loop actually faster than NSPredicate for me, and easier since u can convert it to string or extract it for easy compare, but if u want then the format should be:
#"(%# >= %#) AND (%# <= %#)", dateForCompare, dayStart, dateForCompare, dayEnd where dayStart is the date to compare at 00:00:00, dayEnd is the date to compare at 23:59:59, tweak it base on your needs, u have to do this because a NSDate object contain exact date and time
Hope it help
if you are using timestamp(NSNumber) to store startAt and endAt then you can use predicate as bellow (to fetch all object whose start date is greter then current date)
NSPredicate *pred = [NSPredicate predicateWithFormat:#"startAt > %#", [NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]]];
NSArray *filteredArr = [yourArray filteredArrayUsingPredicate:pred];
and if you are using only date string then you can use
NSPredicate *pred = [NSPredicate predicateWithFormat:#"startAt = %#", #"your date string must be same as formate you saved in array"];
and if you are using only date string then you can use

NSPredicate issue with greater than equals

I have a simple NSPredicate which is not giving correct result
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF.data.squareFootage >= %#", filterSquareFootage];
filteredArray = [[filteredArray filteredArrayUsingPredicate:resultPredicate] mutableCopy];
Strangely this works for all 6000>=250, 7000>=250, 8000>=6000. But as soon as squareFootage==10000 the predicate for 10000>=any smaller value is false.
Note: 10000 for squareFootage is a max value fetched using a UISlider.If i reduce value to any smaller value, the predicate gives true as result
Am i missing something here and using predicate incorrectly?
I assume that your property is stored as string and not as number, therefore
the values are compared as strings:
"10000" < "250" < "6000"
Using NSNumber instead of NSString (or alternatively some scalar type as NSInteger) should fix the problem.
If you cannot change the data type of the squareFootage property, then the following
predicate should work:
[NSPredicate predicateWithFormat:#"SELF.data.squareFootage.intValue >= %d", [filterSquareFootage intValue]]
YOU CAN"T COMPARE STRINGS USING =< . if SELF.data.squareFootage is a NSNumber try converting filterSquareFootage to NSnumber or int value and compare them like that.

NSPredicate: how to treat strings as numbers?

I am building a compound NSPredicate to make a fetch request, using core data on sqlite, within iOS app. Everything already works fine, but I am not being able to include the last condition. The reason is quite simple: I need to check if the value, stored as a string, is within certain float bounds. The problem is that the conditions are checked on alphabetical order basis, and not according its float value. Here it is the code:
NSString * conditionToBeAdded = [NSString stringWithFormat:#" && (%# >= \"""%#\""") && (%# <= \"""%#\""")", propertyName, myMinFloat, propertyName, myMaxFloat];
stringForPredicate = [stringForPredicate stringByAppendingString:conditionToBeAdded];
NSPredicate * myPredicate = [NSPredicate predicateWithFormat:stringForPredicate];
Does anybody know how to build an NSPredicate object able to check string values as numbers, to be used within a coredata request?
Thank you.
Since NSPredicates allow you to use everything which matches Key-Value Coding approach you can call on NSStrings methods like: intValue or floatValue
NSString * conditionToBeAdded = [NSString stringWithFormat:#" && (propertyName.floatValue >= %f ) && (propertyName.floatValue <= %f)", myMinFloat, myMaxFloat];
stringForPredicate = [stringForPredicate stringByAppendingString:conditionToBeAdded];
NSPredicate * myPredicate = [NSPredicate predicateWithFormat:stringForPredicate];
I hope that helps.
You will want to change that string to a number and fix it properly. String searching is SLOW and should be avoided. Putting numbers into strings is a big performance hit with no gain.
As for your actual predicate, you can do a < or > comparison on strings. I suspect all of those quotation marks you have in the predicate are throwing things off. Quotation marks are not needed in predicates unless you are setting a constant which in this case you are not. Remove them and try it again.

Resources