jimbob
jimbob

Reputation: 3298

checking boolean value from Dictionary.

I have a variable called attending being pulled from a JSON feed. After checking what class type the object is being interpreted as I NSLog this:

attending var type is: __NSCFBoolean

This is done using [varname class] to get the class type of the variable.

So I want to see if this is true or false.... so I write this code..:

if([[_events objectAtIndex:indexPath.row] objectForKey:@"attending"] == YES){

However I cant compile it because it gives me a yellow text error saying this:

enter image description here

What am I doing wrong? How can I fix this. Just to add the data in the feed looks like this:

    {
    attendees =         (
    );
    attending = 1;
    date = "2012-09-24 09:11:00";
    id = 504;
    lessonHTML = "somehtml.";
    name = "Sup";
    youtubeId = "http://www.youtube.com/watch?v=j1vb4cND3G0";
},
    {
    attendees =         (
    );
    attending = 1;
    date = "2012-09-24 09:11:00";
    id = 503;
    lessonHTML = "somehtml.";
    name = "Sup";
    youtubeId = "http://www.youtube.com/watch?v=j1vb4cND3G0";
},
    {
    attendees =         (
    );
    attending = 0;
    date = "2012-09-24 09:11:00";
    id = 508;
    lessonHTML = "somehtml.";
    name = "Sup";
    youtubeId = "http://www.youtube.com/watch?v=j1vb4cND3G0";
},
    {
    attendees =         (
    );
    attending = 1;
    date = "2012-09-24 09:11:00";
    id = 509;
    lessonHTML = "somehtml.";
    name = "Sup";
    youtubeId = "http://www.youtube.com/watch?v=j1vb4cND3G0";
},
    {
    attendees =         (
    );
    attending = 0;
    date = "2012-09-24 09:11:00";
    id = 505;
    lessonHTML = "somehtml.";
    name = "Sup";
    youtubeId = "http://www.youtube.com/watch?v=j1vb4cND3G0";
},
    {
    attendees =         (
    );
    attending = 1;
    date = "2012-09-24 09:11:00";
    id = 506;
    lessonHTML = "somehtml.";
    name = "Sup";
    youtubeId = "http://www.youtube.com/watch?v=j1vb4cND3G0";
},

Upvotes: 7

Views: 7639

Answers (2)

AliSoftware
AliSoftware

Reputation: 32681

Boolean in dictionary and other container classes are encapsulated in NSNumber objects (see the NSNumber documentation for more info)

To extract the value you need to send the boolValue message to the object retrieved from the dictionary and compare it to YES/NO:

NSNumber* attendingObject = [[events objectAtIndex:indexPath.row] objectForKey:@"attending"];
if ([attendingObject boolValue] == YES)
{
    ...
}

Read more about values and how they are encapsulated I collection classes in the Apple documentation here

Upvotes: 22

Michael Dautermann
Michael Dautermann

Reputation: 89509

a "BOOL" type is not an Objective C object but instead a C-style type.

It's stored in a dictionary as a "NSNumber" object.

So what you need to do instead of:

if([[_events objectAtIndex:indexPath.row] objectForKey:@"attending"] == YES){

is something like this:

BOOL attending = NO; // assume NO to start with
NSDictionary * lessonDictionary = [_events objectAtIndex: indexPath.row];
if(lessonDictionary)
{
     NSNumber * attendingObject = [lessonDictionary objectForKey: @"attending"];
     if(attendingObject)
     {
          attending = [attendingObject boolValue];
     }
}

Upvotes: 2

Related Questions