Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HannKett

macrumors member
Original poster
Feb 25, 2012
70
0
Europe
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
 

KnightWRX

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
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.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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])
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
...
(only one can be enabled at any given time)
...

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".
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
Code:
#define isEnabled1 (1<<1)
#define isEnabled2 (1<<2)

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.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
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.

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.
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
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.

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.
 

stirfie

macrumors newbie
Feb 11, 2012
7
0
Western Australia
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.