如何将 NSInteger 转换为 NSString 数据类型?

发布于 2024-08-12 20:13:52 字数 207 浏览 6 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一抹微笑 2024-08-19 20:13:53

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

反话 2024-08-19 20:13:53

Obj-C 方式 =):

NSString *inStr = [@(month) stringValue];

Obj-C way =):

NSString *inStr = [@(month) stringValue];
暖树树初阳… 2024-08-19 20:13:53

现代 Objective-C

NSInteger 具有方法 stringValue,甚至可以与文字一起使用

NSString *integerAsString1 = [@12 stringValue];

NSInteger number = 13;
NSString *integerAsString2 = [@(number) stringValue];

非常简单。不是吗?

迅速

var integerAsString = String(integer)

Modern Objective-C

An NSInteger has the method stringValue that can be used even with a literal

NSString *integerAsString1 = [@12 stringValue];

NSInteger number = 13;
NSString *integerAsString2 = [@(number) stringValue];

Very simple. Isn't it?

Swift

var integerAsString = String(integer)
凤舞天涯 2024-08-19 20:13:53

%zd 适用于 NSInteger(%tu 适用于 NSUInteger),在 32 位和 64 位架构上都没有强制转换和警告。我不知道为什么这不是“推荐方式”。

NSString *string = [NSString stringWithFormat:@"%zd", month];

如果您对为什么这样做感兴趣,请参阅这个问题

%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".

NSString *string = [NSString stringWithFormat:@"%zd", month];

If you're interested in why this works see this question.

沉睡月亮 2024-08-19 20:13:53

简单的方法:

NSInteger value = x;
NSString *string = [@(value) stringValue];

这里 @(value) 将给定的 NSInteger 转换为 NSNumber 对象,您可以为其调用所需的函数,<代码>字符串值。

Easy way to do:

NSInteger value = x;
NSString *string = [@(value) stringValue];

Here the @(value) converts the given NSInteger to an NSNumber object for which you can call the required function, stringValue.

千鲤 2024-08-19 20:13:53

在支持 arm64 的情况下进行编译时,不会生成警告:

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];

When compiling with support for arm64, this won't generate a warning:

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
不气馁 2024-08-19 20:13:53

您还可以尝试:

NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];

You can also try:

NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];
萌︼了一个春 2024-08-19 20:13:53

答案已给出,但认为在某些情况下,这也是从 NSInteger 获取字符串的有趣方式

NSInteger value = 12;
NSString * string = [NSString stringWithFormat:@"%0.0f", (float)value];

The answer is given but think that for some situation this will be also interesting way to get string from NSInteger

NSInteger value = 12;
NSString * string = [NSString stringWithFormat:@"%0.0f", (float)value];
浪推晚风 2024-08-19 20:13:53

在这种情况下,NSNumber 可能对你有好处。

NSString *inStr = [NSString stringWithFormat:@"%d", 
                    [NSNumber numberWithInteger:[month intValue]]];

NSNumber may be good for you in this case.

NSString *inStr = [NSString stringWithFormat:@"%d", 
                    [NSNumber numberWithInteger:[month intValue]]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文