user1049603
user1049603

Reputation: 13

Use IntValue in if-statement to calculate?

I'm really new to programming with objective c and so I hope that you can help me. I want to use 3 intValues from textfields to calculate. I have done it like this:

-(int)om{
NSNumber *Om1 = [NSNumber numberWithInt:[self.Textfield1.text intValue]];
return [Om1 intValue];}

-(int)on{
NSNumber *On1 = [NSNumber numberWithInt:[self.Textfield2.text intValue]];
return [On1 intValue];}

-(int)lv{
NSNumber *Lv1 = [NSNumber numberWithInt:[self.Textfield3.text intValue]];
return [Lv1 intValue];}

Now I want to use an if-statement to calculate the sum and run a message on the screen. I want calculate whether Om1 + On1 is less than Lv1+4. Is it a good idea to use the if-statement?

Upvotes: 1

Views: 141

Answers (2)

zaph
zaph

Reputation: 112857

One approach is just to use variables, no need to use NSNumbers. Using variable is perhaps more clear than three methods.

int om = [self.Textfield1.text intValue];
int on = [self.Textfield2.text intValue];
int lv = [self.Textfield3.text intValue];

if ((om + on) < (lv + 4)) {
    // your code here
}

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181310

You don't need to write simpler function. I.E.:

-(int) om {
    return [self.Textfield1.text intValue];
}

There is no point in creating a NSNumber instance just to return value.

You can use an if statement to do what you want:

if (([self om] + [self on]) < ([self lv] + 4)) {
    // your code here
}

Upvotes: 1

Related Questions