Reputation: 196
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
Upvotes: 1
Views: 422
Reputation: 196
Currect Answer is
NSPredicate *p1= [NSPredicate predicateWithFormat:@"(amount >= %d) && (amount <= %d)", text1.intValue, text2.intValue]];
Upvotes: 1
Reputation: 76
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
}
Upvotes: 1
Reputation: 119041
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.
Upvotes: 1