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

时间:2011-08-16 16:11:16

标签: nsdictionary touchjson

我使用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
}

我做错了吗?

我很感激任何帮助。

1 个答案:

答案 0 :(得分:1)

[dic valueForKey:@"dataCount"]将是NSNumber。您需要在其上调用-intValue以获得int

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