PDA

View Full Version : Have a list of BOOL's in an array?




HannKett
Feb 25, 2012, 05:34 AM
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
Feb 25, 2012, 07:29 AM
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;
...

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
Feb 25, 2012, 09:12 AM
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 (!isEnabled[0])

chown33
Feb 25, 2012, 09:53 AM
...
(only one can be enabled at any given time)
...

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

HannKett
Feb 25, 2012, 10:53 AM
Thanks guys :)

now I have some fun stuff to play around with tonight!

ArtOfWarfare
Feb 25, 2012, 12:20 PM
#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
Feb 25, 2012, 12:29 PM
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.


0001
0100


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

robvas
Feb 25, 2012, 06:54 PM
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
Feb 26, 2012, 06:01 AM
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]){
NSLog(@"YES");
}else {
NSLog(@"NO");
}
}

just my 5 cents