7

我从 JSON 提要中提取了一个名为参加的变量。在检查了对象被解释为 I NSLog 的类类型之后:

attending var type is: __NSCFBoolean

这是使用 [varname class] 来获取变量的类类型来完成的。

所以我想看看这是真的还是假的......所以我写了这段代码......:

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

但是我无法编译它,因为它给了我一个黄色文本错误说:

在此处输入图像描述

我究竟做错了什么?我怎样才能解决这个问题。只需在提要中添加数据,如下所示:

    {
    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";
},
4

2 回答 2

22

字典中的布尔值和其他容器类被封装在对象中(有关更多信息,NSNumber请参阅文档)NSNumber

要提取值,您需要将boolValue消息发送到从字典中检索到的对象并将其与 YES/NO 进行比较:

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

在此处的 Apple 文档中阅读有关值以及如何封装 I 集合类的更多信息

于 2012-09-16T21:38:51.103 回答
2

" BOOL" 类型不是 Objective C 对象,而是 C 风格的类型。

它作为“ NSNumber”对象存储在字典中。

所以你需要做的不是:

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

是这样的:

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];
     }
}
于 2012-09-16T21:37:29.437 回答