views:

397

answers:

3

In Objective-C, are there any ways to indicate that an NSNumber* should actually be a BOOL? Right now my code looks like:

NSNumber *audio; // BOOL wrapper

Without the comment, it is not immediately obvious that *audio is a boolean value.

My first thought was to try

typedef NSNumber* BOOL;

but this gave a compiler error apparently because typedef doesn't understand Objective-C.

Without changing the variable names (which is difficult when using existing APIs), how should I indicate that an NSNumber* holds a boolean value?

A: 

The best way would be to call the variable audioBool or something that signifies a boolean value. Comments are good and all, but sometimes they can be missed or just not read.

If you have a problem with renaming many variables, you can use Xcode's refactor tool. Simply right click (or cmd+click) on the variable name, choose the Refactor... option and then rename the variable. Hit Preview, and Xcode will show you what it's about to change. If you like what you see, go ahead and apply, if not, cancel.

Xcode will also give you the option to make a snapshot before committing to a refactor operation. This is on by default. So if anything goes wrong, you can just roll back.

Jasarien
I would, but I'd like to keep variable names consistent with the server side of my application.Also, I prefer to keep information about the variable's type in its type rather than relying on semi-Hungarian notation.
brainfsck
+2  A: 

The code:

typedef NSNumber* BOOL;

doesn't compile because BOOL is already a typedef, and it's not allowed to redefine a typedef.

So you could use another name for that type, e.g.:

typedef NSNumber NSNumberBool;
NSNumberBool *audio;

Or, probably better, name the variable so that you know it is an NSNumber and contains a bool, this way you don't even need to go look for the variable type:

NSNumber *audioNumberBool;
...
[audioNumberBool boolValue];
squelart
"typedef NSNumber NSNumberBool" doesn't compile either
brainfsck
Weird, it works fine for me... Using Xcode 3.1.3.
squelart
Oh it seems that it only won't compile in my MyApp_Prefix.pch file. Do you know why? I have a different typedef working fine in there.
brainfsck
If you put the typedef inside the #ifdef __OBJC__ block at the top of your .pch, it compiles fine. Outside of it, it doesn't work, maybe because NSNumber is only available when __OBJC__ is not defined and the pre-compiler is strict about this...
squelart
A: 

Clearly, the best solution is to rename the property to be something like isAudio or hasAudio. But if you can't do it, then a mediocre solution is the typedef like you describe. The typedef you describe fails because BOOL is already defined in Objective C in objc.h:

typedef signed char BOOL;

beside which, that would be confusing since it doesn't indicate its actually an NSNumber and not just a bool/int. I would suggest something like:

typedef NSNumber NSNumberBool;

or perhaps in this case, better would be:

#define NSNumberBool NSNumber

and then use:

NSNumberBool *audio;

Or just use a comment as you have done.

Peter N Lewis