Have a list of BOOL's in an array?

Discussion in 'iOS Programming' started by HannKett, Feb 25, 2012.

  1. HannKett macrumors member

    Feb 25, 2012
    Hello folks,

    Is it possible to have a long list of bools in an array and then cycle trough those in an switch statement? Or should I use a directory? (These two things I know nothing about).

    basically I have 6 BOOL's and what I do now is:

    isEnabled1 = YES;
    isEnabled2 = NO;
    isEnabled6 = NO;
    (only one can be enabled at any given time)

    so to use these in my app I do
    if (isEnabled1 == YES)
    do this;

    if (isEnabled2 == YES)
    do that;

    but I would like to make it into a switch statement just because it looks nicer.

    switch (myBOOLs)
    case isEnabled1
    case isEnabled2

    is this possible? I played with it yesterday for a bit but did not get it to work (due to my lack of understanding of arrays and nsdirectory).. haven't had time to read up on these yet.

    Thanks for any input on the matter
  2. KnightWRX macrumors Pentium


    Jan 28, 2009
    Quebec, Canada
    A switch ? No. You can't really use "variables" in cases like that. But... This would be much much cleaner :

    #define ENABLE_1 0
    #define ENABLE_2 1
    #define ENABLE_3 2
    BOOL flags[3];
    int flagtocheck;
            case ENABLE_1 : /* do something */ break;
            case ENABLE_2 : /* do something else */ break;
    Or use an enum instead of defines if you want runtime definations rather than compile time.
  3. ArtOfWarfare macrumors G3


    Nov 26, 2007
    A few notes:

    For NSArrays or NSDictionaries, you can only insert subclasses of NSObject. So you couldn't insert a BOOL, but you could insert an NSNumber which contains a BOOL.

    To declare a C-Array named isEnabled with 6 BOOLs, you'd do something like this:

    BOOL isEnabled[6];
    To then access any of your isEnableds, you'd type isEnabled[X] where X is the index of the BOOL in isEnabled you'd like to access (if I'm not mistaken, the first object is at index 0, and the sixth is at index 5. Object Index = Object Number - 1, or its zero-based, in other words.)

    A final note:
    BOOL == 1
    will return TRUE if BOOL is TRUE. Thus you could clean your code up a little by simply having if condition checks that are just
    if (isEnabled[0])
    rather than involving that == 1 in there. If you'd like to see if a BOOL is zero, you could have
  4. chown33 macrumors 604

    Aug 9, 2009
    Then get rid of the 6 BOOLs and make a single int:
    int enabledNum;
    Assign it one of the values 1 thru 6. It can only be one value at a time, so you still have the "only one can be enabled at any given time" behavior.

    And now you can use switch.

    You could also encode the int as a set of bits. Each bit represents a boolean (by nature, a boolean is a single bit). So isEnabled1 occupies bit-position 1, isEnabled2 occupies bit position 2. And so on. Like so:
    #define isEnabled1 (1<<1)
    #define isEnabled2 (1<<2)
    Now you can store the value isEnabled2 (or whatever) into the int enabledNum. And you can use switch.

    However, because only one bool can be active at a time, this is fundamentally no different than simply storing a number 1-6 in an int variable. You're not combining bits, so the bit-per-boolean representation is just pointless complication.

    (I've neglected bit position 0 for illustration purposes. Typically, using bits in an int starts at position 0.)

    You didn't explain what you're actually doing with these 6 bools, which currently aren't even in a list, but have separate individual variable names. So it's possible that using a single int can't replace the functionality you have now. However, if you explain what you're doing, and how you're doing it, then maybe there's a simpler solution.

    By the way, there's no nsdirectory class. It's NSDictionary. A "dictionary" is not a "directory".
  5. HannKett thread starter macrumors member

    Feb 25, 2012
    Thanks guys :)

    now I have some fun stuff to play around with tonight!
  6. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I don't think my knowledge of C is what it should be... what is the << notation stuff you have? I know I've seen it in C++ for cin and cout statements... I feel that this is probably an unrelated use.
  7. subsonix macrumors 68040

    Feb 2, 2008
    It's a bit shift operator, 1 << 2 will shift 1 two places to the left resulting in 4.

    Although, in this case since no variables are involved you might as well just assign 2 and 4 directly.
  8. robvas macrumors 68030

    Mar 29, 2009
    Bitmasks and shifts -


    They used to be very common tricks to fit 8 flags in a single byte. Not sure if the storage savings is worth the speed tradeoff these days. Not sure if there's a speed penalty to using these on modern computers in the first place though.
  9. stirfie macrumors newbie


    Feb 11, 2012
    Western Australia
    You could use a C array in a simple for loop:

        BOOL arrayTest[] = {YES,YES,NO};
        for (int i = 0; i <3; i++) {
            if (arrayTest[i]){
            }else {
    just my 5 cents

Share This Page