Re: BOOL value in Dictionary
Re: BOOL value in Dictionary
- Subject: Re: BOOL value in Dictionary
- From: Ken Thomases <email@hidden>
- Date: Fri, 21 Nov 2008 08:13:21 -0600
On Nov 21, 2008, at 7:48 AM, Richard S. French wrote:
How does one get a boolean value from a dictionary object?
My XML dictionary contains the value </true> under the given key.
Should this be retrieved as NSNumber or some other object?
I am trying to set the state of a checkbox button based on the value
as
below:
Lots of confusion here.
// return syntax coloring switch
BOOL *colorSwitch = NO;
A BOOL is a scalar value. It's not a class. You don't typically
declare a pointer to a BOOL for such simple purposes as you're using
here.
Even if you do declare a pointer-to-BOOL, initializing it with NO
isn't doing what you think it is. You don't have a false boolean
value, you have a null pointer.
colorSwitch = [NSNumber numberWithBOOL:[temp
objectForKey:@"BBLMColorsSyntax"]];
The subexpression [temp objectForKey:@"BBLMColorsSyntax"] already
gives you an object. If the object is what you think it is, then it's
already an NSNumber, so there's no need to try to construct an
NSNumber from it.
Next, even if you did want to construct an NSNumber from it, it's not
a BOOL. So +numberWithBOOL: wouldn't be appropriate. Any non-nil
object pointer would result in a true-valued NSNumber, even if [temp
objectForKey:@"BBLMColorsSyntax"] had given you a false-valued NSNumber.
Third, you can't take the NSNumber which you're constructing (or
retrieving with -objectForKey:) and assign it to colorSwitch, which is
a pointer to BOOL, not a pointer to an object type. Since colorSwitch
ought not be a pointer, you definitely don't want to do that.
NSLog(@"Color: %@", colorSwitch);
Again, colorSwitch isn't an object pointer, so %@ isn't an appropriate
format specifier for it.
if ( colorSwitch == YES) {
As colorSwitch is a pointer-to-BOOL rather than a BOOL, testing it for
equality to YES won't do what you want.
Even if colorSwitch were a BOOL rather than a pointer, your boolean
test should probably not compare for equality to YES. Just test the
value of colorSwitch as the conditional (no comparison necessary).
[colorBox setState:NSOnState];
}
else {
[colorBox setState:NSOffState];
}
You're looking for something like:
BOOL colorSwitch = [[temp objectForKey:@"BBLMColorsSyntax"] boolValue];
if (colorSwitch)
[colorBox setState:NSOnState];
else
[colorBox setState:NSOffState];
The above could be simplified by eliminating the colorSwitch variable
entirely and just putting the expression directly into the 'if'. You
could also use the ternary conditional operator (?:) so there would be
only one invocation of -setState:.
On the other hand, if you want to go belt-and-suspenders, you could
test that the dictionary really contained the type of object you
thought it should:
id colorState = [temp objectForKey:@"BBLMColorsSyntax"];
if ([colorState isKindOfClass:[NSNumber class]] && [colorState
boolValue])
[colorBox setState:NSOnState];
else
[colorBox setState:NSOffState];
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden