ios 将 NSInteger 转换为 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9404201/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:57:48  来源:igfitidea点击:

casting NSInteger into NSString

objective-ciosnsstringstring-formattingnsinteger

提问by saintech

I'm trying to make one string out of several different parts. This below is what i have right now. I don't get any errors in my code but as soon as i run this and do the event that triggers it i get EXC_BAD_ACCESS. Is there another way of casting this NSIntegerinto a NSString?

我试图用几个不同的部分制作一根弦。下面是我现在所拥有的。我的代码中没有任何错误,但是一旦我运行它并执行触发它的事件,我就会得到EXC_BAD_ACCESS。有没有另一种方法可以将其转换NSInteger为 a NSString

NSString *part1, *part2, *tempString;

NSInteger num1;
NSInteger num2;

part1=@"some";
part2=@"text";    

tempString = [NSString stringWithFormat:@"%@%@%@%@", 
              part1,
              (NSString *)num1, 
              part2, 
              (NSString *)num2];

回答by Ben Zotto

A string and an integer are fundamentally different data types, and casting in Objective-C won't do a conversion for you in this case, it'll just lie to the compiler about what's happening (so it compiles) but at runtime it blows up.

字符串和整数是根本不同的数据类型,在这种情况下,Objective-C 中的强制转换不会为您进行转换,它只会向编译器撒谎(因此它会编译),但在运行时它会崩溃向上。

You can embed an integer directly into a format string by using %dinstead of %@:

您可以使用%d代替将整数直接嵌入到格式字符串中%@

    tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];


NSInteger is just a fancy name for a regular "int" (number). An NSString is an object reference to a string object. Some numeric types (int and floating point) can be sort of converted between eachother directly in C like this, but these two aren't inter-operable at all. It sounds like you might be coming from a more permissive language? :)

NSInteger 只是普通“int”(数字)的一个奇特名称。NSString 是对字符串对象的对象引用。一些数字类型(整数和浮点数)可以像这样直接在 C 中相互转换,但这两者根本不能互操作。听起来你可能来自一种更宽容的语言?:)

回答by saintech

answered Feb 22 '12 at 22:34, Ben Zotto:

A string and an integer are fundamentally different data types, and casting in Objective-C won't do a conversion for you in this case, it'll just lie to the compiler about what's happening (so it compiles) but at runtime it blows up.

You can embed an integer directly into a format string by using %d instead of %@:

tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];

NSInteger is just a fancy name for a regular "int" (number). An NSString is an object reference to a string object. Some numeric types (int and floating point) can be sort of converted between eachother directly in C like this, but these two aren't inter-operable at all. It sounds like you might be coming from a more permissive language? :)

Ben Zotto 于 2012 年 2 月 22 日 22:34 回答:

字符串和整数是根本不同的数据类型,在这种情况下,Objective-C 中的强制转换不会为您进行转换,它只会向编译器撒谎(因此它会编译),但在运行时它会崩溃向上。

您可以使用 %d 而不是 %@ 将整数直接嵌入到格式字符串中:

tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];

NSInteger 只是普通“int”(数字)的一个奇特名称。NSString 是对字符串对象的对象引用。一些数字类型(整数和浮点数)可以像这样直接在 C 中相互转换,但这两者根本不能互操作。听起来你可能来自一种更宽容的语言?:)

"Keep in mind that @"%d" will only work on 32 bit. Once you start using NSInteger for compatibility if you ever compile for a 64 bit platform, you should use @"%ld" as your format specifier." by Marc Charbonneau

"请记住,@"%d" 仅适用于 32 位。一旦开始使用 NSInteger 以实现兼容性,如果您为 64 位平台进行编译,则应使用 @"%ld" 作为格式说明符。" 马克·夏博诺

So, solution:

所以,解决办法:

tempString = [NSString stringWithFormat:@"%@%ld%@%ld", part1, (long)num1, part2, (long)num2];

Source: String Programming Guide for Cocoa - String Format Specifiers(Requires iPhone developer registration)

来源:Cocoa 的字符串编程指南 - 字符串格式说明符(需要 iPhone 开发者注册)

回答by Lorenzo B

You have to use %dfor integer and not %@.

您必须使用%dfor integer 而不是%@

So, [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];is the correct code to format your string.

所以,[NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];是格式化字符串的正确代码。

Hope it helps.

希望能帮助到你。

回答by fbrereto

NSIntegers are actual integers - not NSObject-derived objects. Try:

NSIntegers 是实际整数 - 不是NSObject派生对象。尝试:

tempString = [NSString stringWithFormat:@"%@%d%@%d", part1, num1, part2, num2];

The cast trying to force NSIntegerinto NSString*is also dangerous, and probably the actual source of your crash. The compiler is letting you get away with it because it's trusting you know what you're doing with that kind of casting statement.

试图强行NSInteger进入的演员NSString*也很危险,并且可能是您崩溃的实际根源。编译器让你摆脱它,因为它相信你知道你在用那种类型的强制转换语句做什么。

回答by Dmitry S.

NSString* tempString = [NSString stringWithFormat:@"%@%d%d", part1, num1, num2];

回答by atbebtg

try the following:

尝试以下操作:

 tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,(NSString *)num1, part2, (NSString *)num2];

回答by aalesano

NSString *part1, *part2, *tempString;

NSInteger num1;
NSInteger num2;

part1=@"some";
part2=@"text";

tempString = [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2];
NSLog(@"%@",tempString);