Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting int to NSString

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?

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.

What am I doing wrong?

like image 545
N0xus Avatar asked Feb 13 '14 11:02

N0xus


2 Answers

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.

like image 133
Michał Banasiak Avatar answered Oct 04 '22 00:10

Michał Banasiak


If you use 32-bit environment, use this

NSString *rssiString = [NSString stringWithFormat:@"%d", self.selectedBeacon.rssi];

But you cann't use this in 64-bit environment, Because it will give below warning.

Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long'

So use below code, But below will give warning in 32-bit environment.

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.

NSString *rssiString = [NSString stringWithFormat:@"%ld", (long)self.selectedBeacon.rssi];
like image 45
Mani Avatar answered Oct 04 '22 01:10

Mani