2

The below code works, but I'm trying to come up with some math logic and use some binary operator to put in place of this if statement.

NSInteger indexCor = 0;
    for (PFObject *object in self.objects) {
        if (object == custom) {
            if (indexCor < 2) {
                NSLog(@"rodou indexCor <=2.");
                [self.coresConjugadosDict setObject:self.uiColorsArray[0] forKey:object.objectId];
                indexCor++;
            }
            else if (indexCor < 4) {
                [self.coresConjugadosDict setObject:self.uiColorsArray[1] forKey:object.objectId];
                indexCor++;
            }
            else if (indexCor < 6) {
                [self.coresConjugadosDict setObject:self.uiColorsArray[2] forKey:object.objectId];
                indexCor++;
            }
            else if (indexCor < 8) {
                [self.coresConjugadosDict setObject:self.uiColorsArray[3] forKey:object.objectId];
                indexCor++;
            }
            else if (indexCor < 10) {
                [self.coresConjugadosDict setObject:self.uiColorsArray[4] forKey:object.objectId];
                indexCor++;
            }
            else if (indexCor < 12) {
                [self.coresConjugadosDict setObject:self.uiColorsArray[5] forKey:object.objectId];
                indexCor++;
            }
            else {
                [self.coresConjugadosDict setObject:self.uiColorsArray[6] forKey:object.objectId];
                indexCor++;
            }
        }

So basically, some objects are going to be combined with another object. What I need to do is to set one UIColor for each group of two combined objects. I have a NSArray with the UIColors I want to use, and right now, what happens is that each object that meets the specific criteria I add the first color in the array, increment it. This way, each group of two objects will be of a different color.

So, if have 6 objects...

Objects 1,2 = Color 1 Objects 3,4 = Color 2 Objects 5,6 = Color 3

and so on.

I would like to achieve this without the if statement.

Jorge
  • 1,492
  • 1
  • 18
  • 33

1 Answers1

4

It looks like you'd like to make these transformation:

indexCor    uiColorsArray index
--------    -------------------
0, 1        0
2, 3        1
4, 5        2
6, 7        3
8, 9        4
10, 11      5
12 and up   6

This can be achieved by integer-dividing indexCor by 2, and limiting the result to 6, like this:

[self.coresConjugadosDict
    setObject:self.uiColorsArray[min(indexCor++/2, 6)]
    forKey:object.objectId];

Note: you may need to define your own min function to use the above code; the code above will not work correctly with a simple MIN macro, unless you move indexCor++ on a separate line.

Demo of the concept on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Note - you can use the standard `MIN` macro if you move the increment of `indexCor` to its own line. – rmaddy Jul 30 '14 at 17:04
  • Thanks, this is what I was looking for. Although, how can I define my own min function? I'm trying to redefine min macro, but as you stated, it is not working. – Jorge Jul 30 '14 at 17:25
  • @Jorge Take a look at the demo, I defined a `min` function there. – Sergey Kalinichenko Jul 30 '14 at 17:27
  • I'm getting the error: Undefined symbols for architecture armv7: "_min", referenced from: _main in main.o – Jorge Jul 30 '14 at 17:39
  • when trying to test the function on main.m, just like your demo. – Jorge Jul 30 '14 at 17:40
  • 1
    @Jorge This is because I defined `min` with the `inline` marker. Here is [an answer that explains how to do it correctly](http://stackoverflow.com/a/5229551/335858) - you need a definition in a header, and an `extern inline ...` definition in a .m file. If you do not have to use `min` in other files, you could also define it in your .m file above the `@implementation`. – Sergey Kalinichenko Jul 30 '14 at 17:43
  • Perfect! Thanks for the patience. Now, if I wanted to start over the colors `array` after a certain number on `indexCor`, or return the value of `indexCor` to 0 after a limit, could I do that? – Jorge Jul 30 '14 at 18:02
  • 1
    @Jorge Absolutely, you can do that: simply add an `if` that checks `indexCor`, and resets it once it passes the upper limit. – Sergey Kalinichenko Jul 30 '14 at 18:09