Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking boolean value from Dictionary.

Tags:

xcode

ios

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";
},
like image 362
jimbob Avatar asked Sep 16 '12 21:09

jimbob


2 Answers

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

like image 120
AliSoftware Avatar answered Sep 29 '22 14:09

AliSoftware


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];
     }
}
like image 45
Michael Dautermann Avatar answered Sep 29 '22 13:09

Michael Dautermann