Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用AFJSON处理布尔值_Ios_Objective C_Json - Fatal编程技术网

Ios 如何使用AFJSON处理布尔值

Ios 如何使用AFJSON处理布尔值,ios,objective-c,json,Ios,Objective C,Json,我有一些JSON返回如下: "items":[ { "has_instore_image": false } ] NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]); if([item objectForKey:@"has_instore_image"]==0){ NSLog(@"no, there is not an instore image"); }else{ ... BOOL

我有一些JSON返回如下:

"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
  NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
如果我这样输出值:

"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
  NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
我明白了

has_instore_image val: 0
但如果我这样测试:

"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
  NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];
它总是转到else语句。。。六羟甲基三聚氰胺六甲醚。。您建议我如何获得布尔值和测试?我已经通读了这里的BOOL问题,只是感到困惑,这不是我预期的工作


thx

您正在将指针与整数进行比较

 [item objectForKey:@"has_instore_image"]==0
你应该使用

 [item objectForKey:@"has_instore_image"].integerValue==0
还要注意的是
NO
BOOL
等于0


代码中的
NSLog
语句打印一个0,但这仅仅是因为如果将
NSLog
对象作为参数,则调用对象
description

NSDictionary
的实例方法
objectForKey
返回一个
id
,而不是原始值

如果它是JSON中的
boolean
int
float
等类似数字的值,它将被苹果的
NSJSONSerialization
类和iOS中大多数/所有其他常见的JSON解析器序列化为
NSNumber

如果要从中获取
BOOL
值,可以执行以下操作:

"items":[
{
"has_instore_image": false
}
]
NSLog(@"has_instore_image val: %@", [item objectForKey:@"has_instore_image"]);
if([item objectForKey:@"has_instore_image"]==0){
  NSLog(@"no, there is not an instore image");
}else{
...
BOOL has_instore_image = [[item objectForKey:@"has_instore_image"] boolValue];

我建议将这些id类型(从字典返回)保留为NSNumber

 NSNumber *boolNum=(NSNumber*)[item objectForKey:@"has_instore_image"];
之后,您可以从boolNum获得bool值

[boolNum boolValue]
试试这个

if([boolNum boolValue]==NO){
    NSLog(@"no, there is not an instore image");
 }else
{

 }