繁体   English   中英

NSInteger不等于多久?

[英]NSInteger not equal to long?

我正在尝试在Cocoa中解析一些json数据,但遇到了NSInteger数据类型的麻烦。 json字符串具有一些我分配给NSInteger属性的长值。 不幸的是,分配的NSInteger值与long值完全不同。 为什么会这样? NSInteger定义为typedef长的NSInteger。 我可以将long值分配给long属性,但是我只想知道为什么不能将其分配给NSInteger。

    -(void)parseData:(NSData*)data
{
    NSError*err=nil;
    NSDictionary*jsonData=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
    _userID=(NSInteger)[jsonData valueForKeyWithoutNSNull:@"id"];
}

_userID是NSInteger。 从字典中检索的值很长。

您不能简单地将NSNumber (甚至NSString )转换为NSInteger 字典和其他集合类不能存储NSInteger类的原始类型。

假设您的字典包含数字而不是字符串,那么您需要:

NSNumber *number = [jsonData valueForKeyWithoutNSNull:@"id"];
_userID = [number integerValue];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM