I don’t understand why they don’t plow logical operators, read an article, I’m doing everything right, but it compares in its own way, here’s the code:

NSString *State0 = @"0"; NSString *State2 = @"25"; NSString *State3 = @"50"; NSString *State4 = @"80"; if (LabelSchet.text > State0) { LabelState.text = @"Ваш статус: '1'"; } if (LabelSchet.text > State2) { LabelState.text = @"Ваш статус: '2'"; } if (LabelSchet.text > State3) { LabelState.text = @"Ваш статус: '3'"; } if (LabelSchet.text > State4) { LabelState.text = @"Ваш статус: '4'"; } 

There is a number in LabelState - 15. Does it compare in any case what is in LabelState more than in any NSString, what did I do wrong?

  • What types do you compare with each other? From the text of the question goes that int co string or string with string. This is true? - AlexThumb
  • String with string, about int does not say at all) - SuperPonchik
  • That is, you want to compare the values ​​of the two numbers that are written in the object lines? - AlexThumb
  • Yes, apparently it is this)) - Stanislav Pankevich

1 answer 1

So directly compare nizzya. Can be done as follows:

  NSString *string1 = @"15"; NSString *string2 = @"25"; NSComparisonResult result = [string1 compare:string2]; if (result == NSOrderedAscending) { // string1 < string2 } else if (result == NSOrderedDescending) { // string1 > string2 }else if (result == NSOrderedSame) {// string1 == string2 } 

or:

 if (result < 0) {// string1 < string2 } else if (result > 0) { // string1 > string2 } else if (result == 0) {// string1 == string2 } 

and in your case it would be right to bring everything to numbers and compare them already:

 int labelInt = [LabelSchet.text intValue]; 

or

 NSInteger labelInteger = [LabelSchet.text integerValue]; 
  • one
    I think that bringing to numbers is the healthiest option for the vehicle, show him how it is done)) - Stanislav Pankevich
  • one
    plus to bring everything to numbers and compare them already . If you need to work only with numbers, it makes sense not to use NSString at all - AlexThumb
  • one
    answer added. I think this information will be enough to move on :) - zhenyab
  • Thanks a lot, that's what I needed.) - SuperPonchik