比较NSDictionary的valueForKey方法返回值的问题



我使用TouchJSON将以下内容转换为NSDictionary对象

// sample JSON object
{
data =     (
            {
        companyId = 4779;
        companyName = "ABC Corporation";
    },
            {
        companyId = 4806;
        companyName = "EFG Corporation";
    }
);
dataCount = 2;
success = 1;
}
NSString *src = @"http://mysite/app1/companyList.php";
NSURL *url = [[NSURL alloc] initWithString:src];    
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dic = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
int dataCount = (int) [dic valueForKey:@"dataCount"];
// This is where I have problem with. The (dataCount == 1) expression never evaluates to true even if value of dataCount from the JSON object equals to 1.
if ( dataCount == 1 ){
  // do something
} else {
  // do something else
}

我做错什么了吗?

[dic valueForKey:@"dataCount"]将是NSNumber。你需要调用-intValue来获得int

int dataCount = [[dic valueForKey:@"dataCount"] intValue];
if (dataCount == 1) {
    ... 
}

最新更新