如何提取 64 位有符号整数值 [英] how to extract 64 bit signed integer value

查看:28
本文介绍了如何提取 64 位有符号整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询 NSDictionary 的值.因为我遇到了问题,所以我打印了对象.它向我展示了以下内容:

{contents = "myDictionaryItem"} = <CFNumber 0x5d58940 [0x26af380]>{value = +1286301600, type = kCFNumberSInt6codeSInt

所以它是有符号的 64 位整数类型.所以我试图提取这样的值

NSString *myString = [NSString stringWithFormat:@"%qi", [myDictionary objectForKey:@"myDictionaryItem"]];

这给了我一个值,但该值不是字典中的值.

我也试过这个

long long my64bitsignedIntegerValue = (long long)[myDictionary objectForKey:@"myDictionaryItem"];

但我收到警告Cast from pointer to integer of different size.这不起作用,因为 stringWithFormat 正在返回 id 类型的值.我发现这个 post 其中 idUInt32 类型.并且有符号的 64 位整数不是无符号的 32 位整数,所以我收到了这个警告.如果我忽略警告,值仍然是错误的.

我想要的价值是例如1286301600.一个有符号整数(-2.147.483.648 到 2.147.483.647)应该做我想做的事情.我不明白为什么 Cocoa 试图制作一个 64 位有符号整数.

如何提取字典的 64 位有符号整数并将其用作变量?

解决方案

[myDictionary objectForKey:@"myDictionaryItem"]

返回对象(我想 - NSNumber),而不是标量数.如果它确实是一个 NSNUMer 对象,那么您可以使用以下函数之一从中获取一个数字:

-intValue-longValue-longLongValue

以下行必须根据您的号码生成正确的字符串:

NSString *myString = [NSString stringWithFormat:@"%qi", [[myDictionary objectForKey:@"myDictionaryItem"] intValue]];

I'm querying a NSDictionary for a value. Because I got problems I printed the object. It shows me the following:

<CFString 0x5d33630 [0x26af380]>{contents = "myDictionaryItem"} = <CFNumber 0x5d58940 [0x26af380]>{value = +1286301600, type = kCFNumberSInt64Type}

So it is of type signed 64 bit integer. So I tried to extract the value like this

NSString *myString = [NSString stringWithFormat:@"%qi", [myDictionary objectForKey:@"myDictionaryItem"]];

That gives me a value but the value is not the one in the dictionary.

I also tried this

long long my64bitsignedIntegerValue = (long long)[myDictionary objectForKey:@"myDictionaryItem"];

but I get the warning Cast from pointer to integer of different size. That doesn't work because stringWithFormat is returning a value of type id. I found this post where id is of type UInt32. And a signed 64 bit integer is not unsigned 32 bit integer, so I get this warning. If I ignore the warning, the value is still wrong.

The value I would like to have is e.g. 1286301600. A signed integer (−2.147.483.648 to 2.147.483.647) should do the thing I want. I don't understand why Cocoa is trying to make a 64 bit signed Integer.

How can I extract the 64 bit signed integer of the dictionary and use it as a variable?

解决方案

[myDictionary objectForKey:@"myDictionaryItem"]

Returns object (I suppose - NSNumber), not a scalar number. If it is really a NSNUmber object then you can get a number from it using one of the following functions:

-intValue
-longValue
-longLongValue

The following line must produce correct string from your number:

NSString *myString = [NSString stringWithFormat:@"%qi", [[myDictionary objectForKey:@"myDictionaryItem"] intValue]];

这篇关于如何提取 64 位有符号整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆