__NSCFBoolean isEqualToString:]:发送到实例的无法识别的选择器

Var*_*nJi 6 json objective-c

当我从JSON php获取值时,我遇到了__NSCFBoolean isEqualToString的问题:

我的代码如下:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        //convert to JSON
        for(id key in res) {
        id value = [res objectForKey:key];

        NSString *keyAsString = (NSString *)key;
        NSString *valueAsString = (NSString *)value;
        //NSCFBoolean
        /*if([value isKindOfClass:[NSNumber class]])
        {
            printf("Number");
        }
        else if([value isKindOfClass:[NSString class]])
        {
            printf("String --- \n");
            if([value isEqualToString:@"0"])
            {
                printf("Wrong \n");
            }
            else
            {
                printf("Right \n");
            }
        }*/
        NSLog(@"%@",[valueAsString class]);
        if([keyAsString isEqualToString:@"success"])
        {
            printf("Working \n");
        }
        else
        {
            printf("Not Working\n");
        }
        //Error is in below line
        if([valueAsString isEqualToString:@"0"])// strcmp([value objCType], @encode(BOOL)) == 1)
        {
            [self loginSuccess];
        }
        else if(strcmp([value objCType], @encode(BOOL)) == 0)
        {
            [self loginFailure];
        }
        NSLog(@"key: %@", keyAsString);
        NSLog(@"value: %@", valueAsString);
    }
Run Code Online (Sandbox Code Playgroud)

}}

获取的问题[__NSCFBoolean isEqualToString:]:无法识别的选择发送到实例0x1dc9964

从昨天开始搜索谷歌这个问题,但还没有解决方案.NSCFBoolean现在也不能在iOS 6.0中使用.那么我该怎样做才能从JSON字符串中检查"0"或"1"?请帮忙.

Fab*_*ser 19

value是一个NSNumber对象(__ NSCFBoolean,因为NSNumber是一个类集群),而不是一个字符串!使用[value boolValue]以确定它是否是0或1.

投射对象不会改变它的类型!

  • 将`[valueAsString isEqualToString:@"0"]`更改为`[value boolValue] == 0` (5认同)
  • `[value boolValue] == NO`会更好,而`![value boolValue]`会是最好的. (2认同)