当我从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);
    }

}
}

正在将[__NSCFBoolean isEqualToString:]:无法识别的选择器发送到实例0x1dc9964

从昨天开始在Google上搜索此问题,但是还没有解决方案。现在,NSCFBoolean在iOS 6.0中也不可用。那么,我该怎么做才能从JSON字符串中检查“0”或“1”?请帮忙。

最佳答案

value是一个NSNumber对象(__NSCFBoolean,因为NSNumber是一个类集群)而不是字符串!使用[value boolValue]来确定它是0还是1。

转换对象不会改变其类型!

关于objective-c - __NSCFBoolean isEqualToString :]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14662947/

10-09 01:50