Is there a better way to perform this logic in one line?
- (BOOL)isValueInRange {
return ((level.integerValue > 100) || (level.integerValue < 0)) ? NO : YES;
}
You can do:
return level.integerValue >= 0 && level.integerValue <= 100;
This will return true if the value is in the range, false if it is not.
I don't think you can escape having to repeat level.integerValue twice.
return !((level.integerValue > 100) || (level.integerValue < 0))
You can also use NSLocationInRange:
NSLocationInRange(level.integerValue, NSMakeRange(0, 100)
Related
I have tried many thing involving this, >=, >==, =>, ==>.i can not find one that works. hey all return either primary expression needed or expected initializer before '>'. I am creating a IR receiver latch switch and thus have to create parameters for the code because the receiver is not constant in all conditions. Full code below. Any suggestions to fix the code please reply and don't DM me. Thank you.
code:
int LEDState = 0;
int LEDPin = 8;
int dt = 100;
int recieverOld ==> 500 and recieverOld ==< 2000;
int recieverNew;
int recieverPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(recieverPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
recieverNew = digitalRead(recieverPin);
if((recieverOld >== 0 && recieverOld <== 10) && (recieverNew >== 500 && recieverNew <== 2000) {
if(LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
}
recieverOld = recieverNew;
delay(dt);
}
error:
expected initializer before '==' token
if one = used line 4 and related, return error expected primary-expression before '>' token
if > before = line 4 and related, return error expected initializer before '>=' token
Any solutions or suggestions welcome.
TL;DR
Operators that do no exist, and that you should NOT use:
==>, ==<, >==, <==
Operators that works and you can use them:
>= - MORE THAN OR EQUAL, compare operator, for example X >= 5
<= - LESS THAN OR EQUAL, compare operator, for example X <= 5
> - MORE THAN, compare operator, for example X > 5
< - LESS THAN, compare operator, for example X < 5
== - compare operator, when you want to compare values of the variables if they have the same value, for example X == 5, Y == X, 10 == 7
=== - equality operator, similar to compare operator ==, but aditionally checks the type of a variable. for example X === Y, '10' === 10
= - assign operator, when you want to assign something to the variable, for example X = 5
<> OR != - NOT EQUAL, compare operator, for example X != 5, Y <> 10
!== - similar to != or <>, but also checks the type of a value. For example 10 !== '10', and will return opposite result of the equality operator ===
After profiling my app, I appear to have a memory leak somewhere in this method:
- (void) didEvaluateActions {
for (int x = 0; x < self.children.count; x++) {
if ([self.children[x] isKindOfClass:[BallNode class]]) {
BallNode * ball = (BallNode *) self.children[x];
if (abs(ball.physicsBody.velocity.dx) < BALL_MIN_VEL_X) {
ball.physicsBody.velocity = CGVectorMake((ball.physicsBody.velocity.dx < 0 ? -1 : 1) * BALL_MIN_VEL_X, ball.physicsBody.velocity.dy);
}
if (abs(ball.physicsBody.velocity.dx) > BALL_MAX_VEL_X) {
ball.physicsBody.velocity = CGVectorMake((ball.physicsBody.velocity.dx < 0 ? -1 : 1) * BALL_MAX_VEL_X, ball.physicsBody.velocity.dy);
}
if (abs(ball.physicsBody.velocity.dy) < BALL_MIN_VEL_Y) {
ball.physicsBody.velocity = CGVectorMake(ball.physicsBody.velocity.dx, (ball.physicsBody.velocity.dy < 0 ? -1 : 1) * BALL_MIN_VEL_Y);
}
if (abs(ball.physicsBody.velocity.dy) > BALL_MAX_VEL_Y) {
ball.physicsBody.velocity = CGVectorMake(ball.physicsBody.velocity.dx, (ball.physicsBody.velocity.dy < 0 ? -1 : 1) * BALL_MAX_VEL_Y);
}
}
}
}
I'm not seeing where this method could be causing a memory leak. BallNode is not created here, so why is instruments pointing back to this location? How is this method causing a leak?
-Thank in advance
I it doesn't appear that you are doing anything wrong, but if you are pointing the the correct spot in code then I think it has to do with self.children.count
Try
NSUInteger count = self.children.count;
for (NSInteger x = 0; x < count; x++)
{
if ([self.children[x] isKindOfClass:[BallNode class]]) {
BallNode * ball = (BallNode *) self.children[x];
if (abs(ball.physicsBody.velocity.dx) < BALL_MIN_VEL_X) {
ball.physicsBody.velocity = CGVectorMake((ball.physicsBody.velocity.dx < 0 ? -1 : 1) * BALL_MIN_VEL_X, ball.physicsBody.velocity.dy);
}
if (abs(ball.physicsBody.velocity.dx) > BALL_MAX_VEL_X) {
ball.physicsBody.velocity = CGVectorMake((ball.physicsBody.velocity.dx < 0 ? -1 : 1) * BALL_MAX_VEL_X, ball.physicsBody.velocity.dy);
}
if (abs(ball.physicsBody.velocity.dy) < BALL_MIN_VEL_Y) {
ball.physicsBody.velocity = CGVectorMake(ball.physicsBody.velocity.dx, (ball.physicsBody.velocity.dy < 0 ? -1 : 1) * BALL_MIN_VEL_Y);
}
if (abs(ball.physicsBody.velocity.dy) > BALL_MAX_VEL_Y) {
ball.physicsBody.velocity = CGVectorMake(ball.physicsBody.velocity.dx, (ball.physicsBody.velocity.dy < 0 ? -1 : 1) * BALL_MAX_VEL_Y);
}
}
}
The way you have it currently it is trying to evaluate self.children.count every iteration through the loop. This shouldn't cause a leak, but there could be something under the hood preventing it from being released correctly when evaluating it repeatedly.
Also you may want to look at enumerateChildNodesWithName: as a quicker alternative than looking at every child and seeing if it is a BallNode.
Hopefully that is the issue and is easily fixed.
You keep creating more and more instances of your BallNode class. Try adding ball = nil; after your last if statement but honestly I do not know if that will help you.
Either your code logic is wrong or there is other stuff going on which you did not post. Usually it would make sense for you to have an ivar of your ball which you would apply the posted code to.
This is how I would write it in C.
if (x > 50)
{
//Already wrote this
}
else if((x < 50) && (x > 0))
{
if (a < 0.125y)
{return 0;}
else if (z >= 50)
{return 50;}
else
{return x;}
}
else
{
return 0;
}
I tried nesting a ton of iif statements but it got so messy it was unreadable. Is there any more efficient way?
Any help would be wonderful. Thanks!
iif(x>50,[your code],iif(x > 0, iif(a < .125y,0,iif(z >= 50,50,x)),0)
Future note: this does not seem like a huge nested problem. I've seen some with 10 or more levels, even hitting the limit (Somewhere # 10?). One trick I have done for complex issues is create subqueries that do some of the calculation for the main query.
Your easiest bet for something too complex though, is to just create a public function in a module. You can then use that just as you normally would any other evaluation in a query. So...
Public Function ReturnMyValue(x as Integer, a as integer, y as integer) as Integer
'your code here
End Function
Then in your query...
=ReturnMyValue(x,a,y)
Guys what am I doing wrong?
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
}
else if (numberstring.intValue >16 && <=40){
rankLabel.text = #"A2";
}
I get an error on the "<=40" ..
You missed off a variable reference:
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
} // vv here vv
else if (numberstring.intValue >16 && numberstring.intValue <= 40){
rankLabel.text = #"A2";
}
As an optional extra, it looks like numberstring is an NSString object, which you are repeatedly converting to an integer in order to test various ranges. That operation is quite expensive, so you are better off doing the conversion once:
int value = [numberstring intValue];
if (value <=15) {
rankLabel.text = #"A1";
}
else if (value >16 && value <= 40){
rankLabel.text = #"A2";
}
Also note that the intValue method is not a property so I would avoid using the Objective-C 2.0 dot syntax to access it and use the normal method calling mechanism.
The && operator links two clauses together. However, each clause is independent, so each one has to be syntactically correct on its own if the other was removed. If you apply this rule to your condition, you can see that "<=40" is not syntactically correct on its own. Thus you need to reference the value being compared, as follows:
if (numberstring.intValue > 16 &&
numberstring.intValue <= 40) // this is syntactically correct on its own
I have been attempting to create an if else statement that will return a text string based on certain constraints. The first 3 constraints work, but when the event of the final constraint occurs, it triggers the second again. The random number generator occasionally used a 0 value, so I wanted to account for that. I am new to this, and apologize for indenting, etc.
I have been looking around here for a bit and couldn't find anything that seemed to cover this. If I missed it, a hint in the right direction would be appreciated as well.
double txtestimateCategory = [mynum computeVolume];
NSLog(#"The volume is %f", txtestimateCategory);
int v = ((txtestimateCategory * 1));
if ((v >= 8000))
{
NSLog(#"The box is large");
}
else if ((1 <= v < 1000))
{
NSLog(#"The box is small");
}
else if ((1000 <= v < 8000))
{
NSLog(#"The box is medium");
}
else
{
NSLog(#"The box is a lie");
}
Comparators are binary operators. You have to write:
else if (1 <= v && v < 1000)
etc.
(Otherwise you would be evaluating things like true < 1000, and true converts to 1 implicitly. Not what you meant!)