[英]NSInteger not equal to long?
I am trying to parse some json data in Cocoa and I am having troubles with the NSInteger data type. 我正在尝试在Cocoa中解析一些json数据,但遇到了NSInteger数据类型的麻烦。 The json string has some long values that I assign to NSInteger properties.
json字符串具有一些我分配给NSInteger属性的长值。 Unfortunately the assigned NSInteger value differs completely from the long value.
不幸的是,分配的NSInteger值与long值完全不同。 Why is that so?
为什么会这样? NSInteger is defined as typedef long NSInteger.
NSInteger定义为typedef长的NSInteger。 I could have assigned the long value to a long property but I just would like to know why I can't assign it to an 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"];
}
The _userID is a NSInteger. _userID是NSInteger。 The value retrieved from the dictionary is a long.
从字典中检索的值很长。
You can't simply cast an NSNumber
(or even NSString
) to an NSInteger
. 您不能简单地将
NSNumber
(甚至NSString
)转换为NSInteger
。 Dictionaries and other collection classes can't store primitive types like NSInteger
. 字典和其他集合类不能存储
NSInteger
类的原始类型。
Assuming your dictionary contains numbers and not strings then you need: 假设您的字典包含数字而不是字符串,那么您需要:
NSNumber *number = [jsonData valueForKeyWithoutNSNull:@"id"];
_userID = [number integerValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.