xcode 检查字典中的布尔值。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12450875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checking boolean value from Dictionary.
提问by jimbob
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:
我从 JSON 提要中提取了一个名为参加的变量。在检查对象被解释为 I NSLog 的类类型之后:
attending var type is: __NSCFBoolean
This is done using [varname class] to get the class type of the variable.
这是通过使用 [varname class] 获取变量的类类型来完成的。
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:
但是我无法编译它,因为它给了我一个黄色文本错误,说:
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";
},
回答by AliSoftware
Boolean in dictionary and other container classes are encapsulated in NSNumber
objects (see the NSNumber
documentation for more info)
字典中的 Boolean 和其他容器类都封装在NSNumber
对象中(NSNumber
更多信息请参见文档)
To extract the value you need to send the boolValue
message to the object retrieved from the dictionary and compare it to YES/NO:
要提取值,您需要将boolValue
消息发送到从字典中检索到的对象并将其与 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
在此处的 Apple 文档中阅读有关值以及它们如何封装 I 集合类的更多信息
回答by Michael Dautermann
a "BOOL
" type is not an Objective C object but instead a C-style type.
" BOOL
" 类型不是 Objective C 对象,而是 C 样式类型。
It's stored in a dictionary as a "NSNumber
" object.
它作为“ NSNumber
”对象存储在字典中。
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];
}
}