How to test to see if number is in range using Objective-C?
I have a very basic question. I would like to know if here is a built-in function in Objective-C or C to help me find if a specific number it's in a certain ran开发者_开发百科ge. I know that this is probably easy question but still I didn't found an answer. On short terms, would like to avoid using multiple "if"s and "else"s for this test.
NSLocationInRange(c, NSMakeRange(a, (b - a)))
This returns a BOOL if c lies within a and b. However a,b and c must be unsigned int. And this is really not very good looking. So I guess it is far better to compare myself.
c >= a && c <= b
Same way you do in C, C++, Java, C#...
if (theNumber >= SOME_MINIMUM_VALUE && theNumber <= SOME_MAXIMUM_VALUE)
{
// ...
}
That's an "inclusive" range check. Should be easy to figure out how to do an "exclusive" check.
There's no inbuilt function, but there's also no way to do it that's more efficient than two conditions on any architecture I'm familiar with. Any function or macro will ultimately boil down to the same as above.
If you're worried that it'll be slow, then don't. Only worry about performance if you actually see that this is somehow a bottleneck. Premature optimization is not worth your time.
Add this method:
- (BOOL)float:(float)aFloat between:(float)minValue and:(float)maxValue {
if (aFloat >= minValue && aFloat <= maxValue) {
return YES;
} else {
return NO;
}
}
And use it like this:
float myFloat = 3.45;
if ([self float:myFloat between:3 and:4]) {
//Do something
}
This is a very easy solution.
Well I don't believe there is a built in function, but you could write your own in seconds.
simply
return (theInt >= min && theInt <= max);
Doing like so will be interpreted as : c >= a && c < b
NSLocationInRange(c, NSMakeRange(a, (b - a)))
If you want to compare like this : c >= a && c <= b You should do like so :
NSLocationInRange(c, NSMakeRange(a, (b - a) + 1))
NSRange is a structure :
{
location, // Starting point
length // Length from starting point to end range
}
So if you want a range from 5 to 15 included -----> NSMakeRange(5, 11);
*If you are a bit disturbed, just count with your finger from 5 to 15 ;). That's how I do when I get to this point of the night when its hard to think :p
If you are working with Signed int, i advise you to create a Macro ;)
#define MY_AWESOME_MACRO_COMPARE(c, a, b) ((c >= a) && (c <= b))
Hope I helped :)
Cheers ;) !
In Objective-C you can use this simple check:
if (variable >= MINVALUE && variable <= MAXVALUE) {
NSLog(@" the number fits between two numbers");
} else {
NSLog(@" the number not fits between two numbers");
}
BOOL FitsBetweenTwoNumber = NSLocationInRange(variable, NSMakeRange(MINVALUE, (MAXVALUE - MINVALUE)));
if (FitsBetweenTwoNumber) {
NSLog(@" the number fits between two numbers");
} else {
NSLog(@" the number not fits between two numbers");
}
you can use NSPredicate to judge whether a array is in range,if you use CoreData do like:
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", MINVALUE, MAXVALUE];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:context]];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
if (error == nil) {
if(results.count >0) {
NSLog(@" the number fits between two numbers");
} else {
NSLog(@" the number not fits between two numbers");
}
} else if (error) {
NSLog(@"%@", error);
}
if you don't use coreData ,you can also use NSPredicate :
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", MINVALUE, MAXVALUE];
NSArray *results = [@[@(variable)] filteredArrayUsingPredicate:predicate];
if(results.count >0) {
NSLog(@" the number fits between two numbers");
} else {
NSLog(@" the number not fits between two numbers");
}
精彩评论