Reputation: 12107
I'm temped to use an int
, and make 0 == NO
, 1 == YES
, and anything else == undefined.
Obviously there are a million ways to do something like this, but what seems like the best way to you? Concerns I can think of include simplicity and memory footprint (e.g. what if I have a lot of these?).
Another way is to use two BOOLs, one for isDefined
, and one for value
Another way,
typedef enum { CPStatusUndefined, CPStatusAvailable, CPStatusUnavailable } CPStatus;
Edit, the use case is:
I have a yes/no property that is difficult to calculate. When it is being checked, it is checked frequently (by UIMenuController
, OFTEN), but unless the user selects it, it is never checked. The way I chose to deal with this is a tri-type variable. The first time you check, if it is undefined you calculate the yes/no value and return it, after that you just return the yes/no value.
Upvotes: 2
Views: 1110
Reputation: 5736
If you want to conserve the most amount of memory, use a char.
char == 0, false char == 1, true else, undefined.
Obviously, you'll want to initialize it at something like -1.
This is the way obj-c does comparator return values: if 0, they are equal. if positive, a > b if negative, a < b
Same idea as above.
Upvotes: 0
Reputation: 13542
Use an enum. In Objective-C they work just like they do in C/C++
typedef enum {
No = 0,
Yes,
Other
} tri_type;
tri_type myVar = No;
if( myVar == Yes || myVar == Other ) {
// whatever
}
Upvotes: 6
Reputation: 35318
How about NSNumber
, since it can be nil
?
[number boolValue] == YES;
[number boolValue] == NO;
[number boolValue] == nil; // or just number == nil
Upvotes: 1