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

发布于 2024-11-29 14:31:13 字数 931 浏览 2 评论 0原文

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

我做错了什么吗?

我将不胜感激任何帮助。

I use TouchJSON to convert the following to an NSDictionary object

// 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
}

Have I done anything wrong?

I'd appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2024-12-06 14:31:13

[dic valueForKey:@"dataCount"] 将是一个 NSNumber。您需要对其调用 -intValue 来获取 int

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

[dic valueForKey:@"dataCount"] will be an NSNumber. You'll need to call -intValue on it to get an int.

int dataCount = [[dic valueForKey:@"dataCount"] intValue];
if (dataCount == 1) {
    ... 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文