简体   繁体   English

NSInteger不等于多久?

[英]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.

相关问题 使 NSInteger 返回方法等于 NSInteger 参数的最佳方法是什么? - What is the best way to make an NSInteger return method equal NSInteger parameter? NSInteger = NSInteger -1? - NSInteger = NSInteger -1? 隐式转换失去整数精度:'NSInteger'(又名'long')到'int' - Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int' 为什么在32位iOS上未将NSInteger定义为很长? - Why isn't NSInteger defined as long on 32-bit iOS? 我应该如何在Objective-C中声明一个long? NSInteger合适吗? - How should I declare a long in Objective-C? Is NSInteger appropriate? Arc 不允许将“nsinteger”(又名“long”)隐式转换为“nsstring *” - Implicit Conversion of 'nsinteger' (aka 'long') to 'nsstring *' is Disallowed with Arc 将 NSInteger 连接到 NSInteger - Concatenate a NSInteger to NSInteger 如何禁用“ NSInteger类型的值”不应用作格式参数; 而是在“ long”中添加明确的强制转换” - How to disable “Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead” 指向整数转换的不兼容指针从结果类型为“NSInteger”(又名“long”)的函数返回“id _Nullable” - Incompatible pointer to integer conversion returning 'id _Nullable' from a function with result type 'NSInteger' (aka 'long') Xcode 8.0 beta 6,隐式转换失去整数精度:从'NSInteger'(aka'long')到'CGSWindow'(aka'int') - Xcode 8.0 beta 6, Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'CGSWindow' (aka 'int')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM