Question

Following code:

usepassive = [agencydata valueForKey:@"passive"];
                NSLog(@"agencydata usepassive: %@",[agencydata valueForKey:@"passive"]);
                NSLog(@"vardata usepassive: %hhd",usepassive);

Produces this output:

2014-05-13 21:35:41.424 Stockuploader[957:303] agencydata usepassive: 1
2014-05-13 21:35:41.425 Stockuploader[957:303] vardata usepassive: 7

I would expect it to be 1 and 1, but it is 1 and 7.

usepassive is declared like this BOOL usepassive; in my .h file.

what is wrong?

Était-ce utile?

La solution

You can't store BOOL objects in a dictionary (or other collections) without first wrapping it in an NSNumber. This means that when you get the value from the dictionary, you are getting the NSNumber. You need to convert this to BOOL.

You can do:

// Assuming usepasive is defined as BOOL
usepassive = [[agencydata objectForKey:@"passive"] boolValue];
NSLog(@"agencydata usepassive: %@", [agencydata objectForKey:@"passive"]); // logs the NSNumber
NSLog(@"vardata usepassive: %hhd", usepassive); // logs the BOOL

Also consider modern syntax:

usepassive = [agencydata[@"passive"] boolValue];
NSLog(@"agencydata usepassive: %@", agencydata[@"passive"]); // logs the NSNumber
NSLog(@"vardata usepassive: %hhd", usepassive); // logs the BOOL
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top