Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSInteger不等于long?_Objective C_Json_Cocoa_Long Integer_Nsinteger - Fatal编程技术网

Objective c NSInteger不等于long?

Objective 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;

我试图在Cocoa中解析一些json数据,但在NSInteger数据类型方面遇到了问题。json字符串有一些长值,我分配给NSInteger属性。不幸的是,指定的NSInteger值与long值完全不同。为什么会这样?NSInteger定义为typedef long 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];

用真实代码和真实数据更新您的问题。清楚你看到的值是什么。我添加了一个parse方法的片段,希望能有所帮助。是的,你是对的,我完全忘记了,谢谢:)。@ok404很高兴我能帮上忙。请不要忘记接受答案,以表明它解决了您的问题。