Reputation: 11830
I have this code
if ([args valueForKey:@"showSetupScreen"]) {
BOOL showSetupScreen = [args valueForKey:@"showSetupScreen"];
NSLog(showSetupScreen ? @"YES" : @"NO");
// meetingConfig.showSetupScreen = showSetupScreen;
}
Where args
is NSMutableDictionary
.
args
value in my dictionary is NO
but when I set to BOOL showSetupScreen = [args valueForKey:@"showSetupScreen"];
it changes into YES
Can someone help me in comprehending why this could be happening.
Attached Screenshot for your reference
Upvotes: 0
Views: 335
Reputation: 413
[args valueForKey:@"showSetupScreen"]
statement returns pointer (address in memory) and it has two options: some address (non zero value) and NULL
(zero). For C programming language true
is any non zero value (any address in memory in our case). And for this reason you get true
in if
operator and in showSetupScreen
variable. But it only tells you that there is some object in the dictionary for the specified key, but not the value of this key (the value wrapped in this object). To get this value (BOOL
in our case), you must call the boolValue
.
Upvotes: 0
Reputation: 437452
A NSDictionary
(or NSMutableDictionary
) cannot directly contain a primitive C type, such as BOOL
. Primitive numeric types (including Boolean) in NSDictionary
are wrapped in NSNumber
objects. See Numbers Are Represented by Instances of the NSNumber Class and Most Collections Are Objects.
Thus, use NSNumber
method boolValue
to extract the Boolean from the NSNumber
, e.g.,
BOOL showSetupScreen = [[args valueForKey:@"showSetupScreen"] boolValue];
Or, more simply:
BOOL showSetupScreen = [args[@"showSetupScreen"] boolValue];
E.g., examples with primitive C types, including BOOL
, NSInteger
, and double
:
NSDictionary *args = @{
@"foo": @NO,
@"bar": @YES,
@"baz": @42,
@"qux": @3.14
};
BOOL foo = [args[@"foo"] boolValue]; // NO/false
BOOL bar = [args[@"bar"] boolValue]; // YES/true
NSInteger baz = [args[@"baz"] integerValue]; // 42
double qux = [args[@"qux"] doubleValue]; // 3.14
For what it's worth, if you expand the values contained within args
, that will show you the internal types for those values, and you will see that that the value associated with showSetupScreen
(or foo
in my example), is not a BOOL
, but rather a pointer to a __NSCFBoolean
/NSNumber
:
Upvotes: 3