简体   繁体   English

将NSInteger转换为NSString

[英]casting NSInteger into NSString

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 . 我的代码没有任何错误,但是一旦我运行它并执行触发它的事件,我就会得到EXC_BAD_ACCESS Is there another way of casting this NSInteger into a NSString ? 还有另一种将NSInteger转换为NSString吗?

NSString *part1, *part2, *tempString;

NSInteger num1;
NSInteger num2;

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

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

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 %d instead of %@ : 您可以使用%d而不是%@将整数直接嵌入格式字符串中:

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

NSInteger is just a fancy name for a regular "int" (number). NSInteger只是常规“ int”(数字)的奇特名称。 An NSString is an object reference to a string object. NSString是对字符串对象的对象引用。 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. 某些数字类型(int和浮点数)可以像这样直接在C语言中相互转换,但是这两种类型根本不能互操作。 It sounds like you might be coming from a more permissive language? 听起来您可能来自更宽松的语言? :) :)

answered Feb 22 '12 at 22:34, Ben Zotto: Ben Zotto在2012年2月22日22:34回答:

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 %d instead of %@: 您可以使用%d而不是%@将整数直接嵌入格式字符串中:

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

NSInteger is just a fancy name for a regular "int" (number). NSInteger只是常规“ int”(数字)的奇特名称。 An NSString is an object reference to a string object. NSString是对字符串对象的对象引用。 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. 某些数字类型(int和浮点数)可以像这样直接在C语言中相互转换,但是这两种类型根本不能互操作。 It sounds like you might be coming from a more permissive language? 听起来您可能来自更宽松的语言? :) :)

"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." “请记住,@“%d”只能在32位上运行。一旦开始使用NSInteger进行兼容性(如果曾经针对64位平台进行编译),则应使用@“%ld”作为格式说明符。 by Marc Charbonneau 通过马克夏邦诺(Marc Charbonneau)

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) 资料来源: 可可字符串编程指南-字符串格式说明符 (需要iPhone开发人员注册)

You have to use %d for integer and not %@ . 您必须对整数使用%d而不是%@

So, [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2]; 因此, [NSString stringWithFormat:@"%@%d%@%d", part1,num1, part2, num2]; is the correct code to format your string. 是格式化字符串的正确代码。

Hope it helps. 希望能帮助到你。

NSString *part1, *part2, *tempString;

NSInteger num1;
NSInteger num2;

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

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

尝试以下方法:

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

NSInteger s are actual integers - not NSObject -derived objects. NSInteger是实际的整数,而不是NSObject派生的对象。 Try: 尝试:

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

The cast trying to force NSInteger into NSString* is also dangerous, and probably the actual source of your crash. 试图将NSInteger强制转换为NSString*强制转换也很危险,并且可能是崩溃的实际原因。 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. 编译器让您摆脱它,因为它信任您知道您使用这种类型的转换语句正在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM