如何将 NSInteger 转换为 NSString 数据类型?
如何将 NSInteger
转换为 NSString
数据类型?
我尝试了以下方法,其中月份是 NSInteger
:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
How does one convert NSInteger
to the NSString
datatype?
I tried the following, where month is an NSInteger
:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
NSInteger 不是对象,您可以将它们转换为
long
,以便匹配当前 64 位架构的定义:NSString *inStr = [NSString stringWithFormat: @"%ld", (long )月];
NSIntegers are not objects, you cast them to
long
, in order to match the current 64-bit architectures' definition:NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];
Obj-C 方式 =):
Obj-C way =):
现代 Objective-C
NSInteger
具有方法stringValue
,甚至可以与文字一起使用非常简单。不是吗?
迅速
Modern Objective-C
An
NSInteger
has the methodstringValue
that can be used even with a literalVery simple. Isn't it?
Swift
%zd
适用于 NSInteger(%tu
适用于 NSUInteger),在 32 位和 64 位架构上都没有强制转换和警告。我不知道为什么这不是“推荐方式”。如果您对为什么这样做感兴趣,请参阅这个问题。
%zd
works for NSIntegers (%tu
for NSUInteger) with no casts and no warnings on both 32-bit and 64-bit architectures. I have no idea why this is not the "recommended way".If you're interested in why this works see this question.
简单的方法:
这里
@(value)
将给定的NSInteger
转换为NSNumber
对象,您可以为其调用所需的函数,<代码>字符串值。Easy way to do:
Here the
@(value)
converts the givenNSInteger
to anNSNumber
object for which you can call the required function,stringValue
.在支持
arm64
的情况下进行编译时,不会生成警告:When compiling with support for
arm64
, this won't generate a warning:您还可以尝试:
You can also try:
答案已给出,但认为在某些情况下,这也是从 NSInteger 获取字符串的有趣方式
The answer is given but think that for some situation this will be also interesting way to get string from NSInteger
在这种情况下,NSNumber 可能对你有好处。
NSNumber may be good for you in this case.