C语言 如何使用Objective-C测试数字是否在范围内?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5223039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test to see if number is in range using Objective-C?
提问by Valentin Radu
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 range. 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.
我有一个非常基本的问题。我想知道这里是否有 Objective-C 或 C 中的内置函数来帮助我查找特定数字是否在某个范围内。我知道这可能是一个简单的问题,但我仍然没有找到答案。就短期而言,希望避免在此测试中使用多个“if”和“else”。
回答by taskinoor
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 和 b 内,则返回 BOOL。但是 a,b 和 c 必须是 unsigned int。而且这真的不是很好看。所以我想自己比较一下要好得多。
c >= a && c <= b
回答by Jonathan Grynspan
Same way you do in C, C++, Java, C#...
与您在 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.
如果您担心它会变慢,那就不要。如果您确实看到这在某种程度上是瓶颈,则只需担心性能。过早的优化不值得你花时间。
回答by Fabio Poloni
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.
这是一个非常简单的解决方案。
回答by Yacine Salhi
Doing like so will be interpreted as : c >= a && c < b
这样做将被解释为: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 :
如果你想这样比较: c >= a && c <= b 你应该这样做:
NSLocationInRange(c, NSMakeRange(a, (b - a) + 1))
NSRange is a structure :
NSRange 是一个结构体:
{
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);
因此,如果您想要包含 5 到 15 的范围 -----> 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
*如果您有点不安,只需用手指从 5 数到 15 ;)。当我到了难以思考的夜晚时,我就是这样做的:p
If you are working with Signed int, i advise you to create a Macro ;)
如果您正在使用 Signed int,我建议您创建一个宏 ;)
#define MY_AWESOME_MACRO_COMPARE(c, a, b) ((c >= a) && (c <= b))
Hope I helped :)
希望我有所帮助:)
Cheers ;) !
干杯;)!
回答by ElonChan
In Objective-C you can use this simple check:
在 Objective-C 中,你可以使用这个简单的检查:
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:
您可以使用 NSPredicate 来判断数组是否在范围内,如果您使用 CoreData,请执行以下操作:
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 :
如果你不使用 coreData ,你也可以使用 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");
}
回答by ultifinitus
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);

