Have a list of BOOL's in an array?

Discussion in 'iPhone/iPad Programming' started by HannKett, Feb 25, 2012.

  1. macrumors member

    Joined:
    Feb 25, 2012
    Location:
    Europe
    #1
    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
    break;
    case isEnabled2
    break;
    .....

    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. macrumors Pentium

    KnightWRX

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

    Code:
    #define ENABLE_1 0
    #define ENABLE_2 1
    #define ENABLE_3 2
    
    ...
    
    BOOL flags[3];
    int flagtocheck;
    ...
    
    if(flags[flagtocheck])
        switch(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. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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:

    Code:
    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:
    Code:
    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
    Code:
    if (isEnabled[0])
    rather than involving that == 1 in there. If you'd like to see if a BOOL is zero, you could have
    Code:
    (!isEnabled[0])
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    Then get rid of the 6 BOOLs and make a single int:
    Code:
    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:
    Code:
    #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. thread starter macrumors member

    Joined:
    Feb 25, 2012
    Location:
    Europe
    #5
    Thanks guys :)

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

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    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. macrumors 68040

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

    Code:
    0001
    0100
    
    Although, in this case since no variables are involved you might as well just assign 2 and 4 directly.
     
  8. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #8
    Bitmasks and shifts -

    http://www.vipan.com/htdocs/bitwisehelp.html

    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. macrumors newbie

    stirfie

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

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

Share This Page