Objective c NSInteger不等于long?
我试图在Cocoa中解析一些json数据,但在NSInteger数据类型方面遇到了问题。json字符串有一些长值,我分配给NSInteger属性。不幸的是,指定的NSInteger值与long值完全不同。为什么会这样?NSInteger定义为typedef long NSInteger。我可以将long值分配给long属性,但我只想知道为什么不能将其分配给NSIntegerObjective c NSInteger不等于long?,objective-c,json,cocoa,long-integer,nsinteger,Objective C,Json,Cocoa,Long Integer,Nsinteger,我试图在Cocoa中解析一些json数据,但在NSInteger数据类型方面遇到了问题。json字符串有一些长值,我分配给NSInteger属性。不幸的是,指定的NSInteger值与long值完全不同。为什么会这样?NSInteger定义为typedef long NSInteger。我可以将long值分配给long属性,但我只想知道为什么不能将其分配给NSInteger -(void)parseData:(NSData*)data { NSError*err=nil;
-(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];
用真实代码和真实数据更新您的问题。清楚你看到的值是什么。我添加了一个parse方法的片段,希望能有所帮助。是的,你是对的,我完全忘记了,谢谢:)。@ok404很高兴我能帮上忙。请不要忘记接受答案,以表明它解决了您的问题。