objective-c 在 NSString 中存储和检索 unsigned long long 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1181637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing and retrieving unsigned long long value to/from NSString
提问by lostInTransit
I have an unsigned long long value which I want to store into an NSString and retrieve from the string.
我有一个 unsigned long long 值,我想将其存储到 NSString 中并从字符串中检索。
Initially I have the value in an NSNumber and I am using this to get the string
最初我在 NSNumber 中有值,我用它来获取字符串
NSString *numStr = [NSString stringWithFormat:@"%llu", [myNum unsignedLongLongValue]];
where myNum is an NSNumber.
其中 myNum 是一个 NSNumber。
To get back the NSNumber from the NSString I have to first get the unsigned long long value. But there is no method in the NSString class to do that (we just have one for getting the long long value, not the unsigned long long value).
要从 NSString 取回 NSNumber,我必须首先获得 unsigned long long 值。但是 NSString 类中没有方法可以做到这一点(我们只有一个用于获取 long long 值,而不是 unsigned long long 值)。
Can someone please tell me how I can get back the value into an NSNumber variable.
有人可以告诉我如何将值恢复到 NSNumber 变量中。
Thanks.
谢谢。
回答by johne
There are a lot of ways to accomplish this. The following is the most pragmatic:
有很多方法可以实现这一点。以下是最实用的:
NSString *numStr = [NSString stringWithFormat:@"%llu", [myNum unsignedLongLongValue]];
// .. code and time in between when numStr was created
// .. and now needs to be converted back to a long long.
// .. Therefore, numStr used below does not imply the same numStr above.
unsigned long long ullvalue = strtoull([numStr UTF8String], NULL, 0);
This makes a few reasonable assumptions such as numStrwill only contain numeric digits and it contains a 'valid' unsigned long long value. A drawback to this approach is that UTF8Stringcreates what essentially amounts to [[numStr dataUsingEncoding:NSUTF8StringEncoding] bytes], or in other words something along the lines of 32 bytes of autoreleased memory per call. For the vast majority of uses, this is no problem what-so-ever.
这做出了一些合理的假设,例如numStr只包含数字并且它包含一个“有效”的 unsigned long long 值。这种方法的一个缺点是UTF8String创建了本质上相当于 的[[numStr dataUsingEncoding:NSUTF8StringEncoding] bytes]东西,或者换句话说,每次调用都会产生 32 字节的自动释放内存。对于绝大多数用途,这从来都不是问题。
For an example of how to add something like unsignedLongLongValueto NSStringthat is both very fast and uses no autoreleased memory as a side effect, take a look at the end of my (long) answer to this SO question. Specifically the example implementation of rklIntValue, which would require only trivial modifications to implement unsignedLongLongValue.
有关如何添加类似的例子unsignedLongLongValue来NSString,既非常快,它不使用自动释放内存的副作用,看看我(长)回答结束这太问题。特别是 的示例实现rklIntValue,它只需要简单的修改即可实现unsignedLongLongValue。
More information regarding strtoullcan be found in its man page.
有关更多信息,请参strtoull见其手册页。

