0

I have a section of code, in which I want a different accessoryView for the TableView cell, based off the number for that cell's entry. The code I have set up is:

NSInteger warriors = [entry.prayerWarriors intValue];

        if (warriors == 0) {
            //Do nothing
            //NSLog(@"0");
        }
        else if (0 < warriors < 50) {
            cell.accessoryView = firstLevel;
           // NSLog(@"50");

        }
        else if (51 < warriors < 100) {
            cell.accessoryView = secondLevel;
          //  NSLog(@"100");

        }
        else if (101 < warriors < 500) {
            cell.accessoryView = thirdLevel;
         //   NSLog(@"500");

        }
        else {
            cell.accessoryView = fourthLevel;
         //   NSLog(@"A Lot");

        }

However, it always returns only the first entry for warriors == 0. What am I doing wrong?

user717452
  • 33
  • 14
  • 73
  • 149
  • try `NSLog`ing the variable rather than a string to see what is in there. `NSLog(@"%@", warriors)` – martskins May 08 '14 at 20:26
  • @lascort ok, I logged the variable, and it came back as expected...5 different numbers, one for each range of numbers. The NSLog for the ranges shows either 0 or 50 (the first two if statements), but none of the others, despite the other 3 variables being from the remaining statements. – user717452 May 08 '14 at 20:29
  • 2
    You've fallen into the trap of using the "natural" `x < y < z` notation for checking between bounds. That does not work (at least not how you think it should) for C or C++ or Java. – Hot Licks May 08 '14 at 20:34

5 Answers5

2

Rather than doing this...

else if (0 < warriors < 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");
}

do this...

else if (0 < warriors && warriors < 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");
}

EDIT

To answer your comment...you probably mean to have some <= or >= in there, as it's going to the last else when warriors equals the border of your if conditionals (50, 100 or 500).

You probably want it to look like this...

 NSInteger warriors = [entry.prayerWarriors intValue];

    if (warriors == 0) {
        //Do nothing
        //NSLog(@"0");
    }
    else if (0 < warriors && warriors <= 50) {
        cell.accessoryView = firstLevel;
        // NSLog(@"50");

    }
    else if (50 < warriors && warriors <= 100) {
        cell.accessoryView = secondLevel;
        //  NSLog(@"100");

    }
    else if (100 < warriors && warriors <= 500) {
        cell.accessoryView = thirdLevel;
        //   NSLog(@"500");

    }
    else {
        cell.accessoryView = fourthLevel;
        //   NSLog(@"A Lot");

    }
user717452
  • 33
  • 14
  • 73
  • 149
martskins
  • 2,920
  • 4
  • 26
  • 52
  • I have changed my code to `NSInteger warriors = [entry.prayerWarriors intValue]; if (warriors == 0) { } else if (0 < warriors && warriors < 50) { cell.accessoryView = firstLevel; } else if (51 < warriors && warriors < 100) { cell.accessoryView = secondLevel; } else if (101 < warriors && warriors < 500) { cell.accessoryView = thirdLevel; } else { cell.accessoryView = fourthLevel; } `But it doesn't see the 3rd or 4th ones. – user717452 May 08 '14 at 20:42
  • maybe your missing some <= or >= ? the way you have it there it's going to the else when warriors equals either 100, 50, 500 – martskins May 08 '14 at 20:44
  • You have breaks where nothing will be true every 50 warriors. E.g. if warriors is 50, then warriors < 50 and 51 < warriors are both false, so the default else at the end will be used. You probably want 50 <= warriors, etc. – David Berry May 08 '14 at 20:45
  • Please note that the first level has a range of 51 (0-50) while the other levels has a range of 50 (i. e. 51-100). Do you want that? – Amin Negm-Awad May 13 '14 at 05:20
1

The statement

if (0 < warriors < 50)

evaluates different than you might think. The first part

(0 < warriors)

evaluates as a boolean, and that boolean will be compared to 50.

So, you need to do: if (0 < warriors && warriors < 50)

augustzf
  • 2,385
  • 1
  • 16
  • 22
0

The other answers make good points about the later cases needing a logical and but if you are getting stuck at if (warriors == 0) { most likely your object entry.prayerWarriors is nil. Put a break point and print it out. (and print out it's class to make sure its as expected)

Also minor but a good habit to be in your conversion of what I'm guessing is an NSNumber isn't using the same type as your variable. Since you are writing into a NSInteger you should replace intValue with integerValue

Anthony
  • 2,867
  • 1
  • 17
  • 17
0

For clarity, I prefer to put the variable first in each condition and to encapsulate each part of the condition:

if (warriors == 0) {
 //Do nothing
NSLog(@"0");
}
else if ((warriors > 0) && (warriors < 50))
{
cell.accessoryView = firstLevel;
NSLog(@"50");
}
else if ((warriors > 51) && (warriors < 100)) {
 cell.accessoryView = secondLevel;
 NSLog(@"100");
    }
    else if ((warriors > 101) && (warriors < 500)) {
        cell.accessoryView = thirdLevel;
        NSLog(@"500");
    }
    else {
        cell.accessoryView = fourthLevel;
        NSLog(@"A Lot");
    }

Just be sure you've have enough parens around the conditions.

GlennRay
  • 959
  • 9
  • 18
  • for clarity, you might want to double check how you indent your code! and all those double parenthesis are just way too much information for my eyes. Just a personal opinion though – martskins May 08 '14 at 20:54
0

You do not need an if cascade for this:

You can store the levels one time anywhere

NSArray *levels = @[firstLevel, secondLevel, thirdLevel, fourthLevel];

And use it indexed:

if( warriors > 0) {
  cell.accessoryView = levels[(warriors-1) / 50]
}

But if you want to have an if cascade, you do not have to double check:

NSInteger warriors = [entry.prayerWarriors intValue];

if (warriors == 0) {
    //Do nothing
    //NSLog(@"0");
}
else if (warriors <= 50) {
    cell.accessoryView = firstLevel;
    // NSLog(@"50");

}
else if (warriors <= 100) {
    cell.accessoryView = secondLevel;
    //  NSLog(@"100");

}
else if (warriors <= 500) {
    cell.accessoryView = thirdLevel;
    //   NSLog(@"500");

}
else {
    cell.accessoryView = fourthLevel;
    //   NSLog(@"A Lot");

}

If you are in an else, it is already tested that the preceding condition failed.(That's the meaning of else.)

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50