ios 将 int 转换为 NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21753259/
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
Converting int to NSString
提问by N0xus
I thought I had nailed converting an int
to and NSString
a while back, but each time I run my code, the program gets to the following lines and crashes. Can anyone see what I'm doing wrong?
我以为我已经确定了转换int
为和NSString
一段时间,但是每次我运行我的代码时,程序都会转到以下几行并崩溃。谁能看到我做错了什么?
NSString *rssiString = (int)self.selectedBeacon.rssi;
UnitySendMessage("Foo", "RSSIValue", [rssiString UTF8String] );
These lines should take the rssi value (Which is an NSInt) convert it to a string, then pass it to my unity object in a format it can read.
这些行应该采用 rssi 值(这是一个 NSInt)将其转换为字符串,然后以它可以读取的格式将其传递给我的统一对象。
What am I doing wrong?
我究竟做错了什么?
回答by Micha? Banasiak
NSString *rssiString = [NSString stringWithFormat:@"%d", self.selectedBeacon.rssi];
UPDATE: it is important to remember there is no such thing as NSInt. In my snippet I assumed that you meant NSInteger.
更新:重要的是要记住没有像 NSInt 这样的东西。在我的代码段中,我假设您的意思是 NSInteger。
回答by Mani
If you use 32-bit environment, use this
如果你使用32位环境,使用这个
NSString *rssiString = [NSString stringWithFormat:@"%d", self.selectedBeacon.rssi];
But you cann't use this in 64-bit environment, Because it will give below warning.
但是你不能在 64 位环境中使用它,因为它会给出以下警告。
Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long'
“NSInteger”类型的值不应用作格式参数;向“long”添加显式转换
So use below code, But below will give warning in 32-bit environment.
所以使用下面的代码,但下面会在 32 位环境中给出警告。
NSString *rssiString = [NSString stringWithFormat:@"%ld", self.selectedBeacon.rssi];
If you want to code for both(32-bit & 64-bit) in one line, use below code. Just casting.
如果您想在一行中同时编码(32 位和 64 位),请使用以下代码。只是铸造。
NSString *rssiString = [NSString stringWithFormat:@"%ld", (long)self.selectedBeacon.rssi];
回答by Jidong Chen
I'd like to provide a sweet way to do this job:
我想提供一种很好的方式来完成这项工作:
//For any numbers.
int iValue;
NSString *sValue = [@(iValue) stringValue];
//Even more concise!
NSString *sValue = @(iValue).stringValue;
回答by codercat
NSString *rssiString = [self.selectedBeacon.rssi stringValue];
For simple conversions of basic number values, you can use a technique called casting. A cast forces a value to perform a conversion based on strict rules established for the C language. Most of the rules dictate how conversions between numeric types (e.g., long and short versions of int and float types) are to behave during such conversions.
对于基本数值的简单转换,您可以使用一种称为强制转换的技术。强制转换根据为 C 语言建立的严格规则强制一个值执行转换。大多数规则规定了数字类型之间的转换(例如,int 和 float 类型的 long 和 short 版本)在此类转换期间的行为方式。
Specify a cast by placing the desired output data type in parentheses before the original value. For example, the following changes an int to a float:
通过将所需的输出数据类型放在原始值之前的括号中来指定强制转换。例如,以下将 int 更改为 float:
float myValueAsFloat = (float)myValueAsInt;
One of the rules that could impact you is that when a float or double is cast to an int, the numbers to the right of the decimal (and the decimal) are stripped off. No rounding occurs. You can see how casting works for yourself in Workbench by modifying the runMyCode: method as follows:
可能影响您的规则之一是,当 float 或 double 转换为 int 时,小数点(和小数点)右侧的数字将被剥离。不发生舍入。您可以通过如下修改 runMyCode: 方法,在 Workbench 中查看转换是如何工作的:
- (IBAction)runMyCode:(id)sender {
double a = 12345.6789;
int b = (int)a;
float c = (float)b;
NSLog(@"\ndouble = %f\nint of double = %d\nfloat of int = %f", a, b, c);
}
the console reveals the following log result:
控制台显示以下日志结果:
double = 12345.678900
int of double = 12345
float of int = 12345.000000
original link is http://answers.oreilly.com/topic/2508-how-to-convert-objective-c-data-types-within-ios-4-sdk/
原始链接是http://answers.oreilly.com/topic/2508-how-to-convert-objective-c-data-types-within-ios-4-sdk/
回答by FluffulousChimp
If self.selectedBeacon.rssi
is an int
, and it appears you're interested in providing a char *
string to the UnitySendMessage
API, you could skip the trip through NSString
:
如果self.selectedBeacon.rssi
是int
,并且您似乎有兴趣向API提供char *
字符串,则UnitySendMessage
可以跳过此过程NSString
:
char rssiString[19];
sprintf(rssiString, "%d", self.selectedBeacon.rssi);
UnitySendMessage("Foo", "RSSIValue", rssiString );