N0xus
N0xus

Reputation: 2724

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?

Upvotes: 6

Views: 22361

Answers (5)

Jidong Chen
Jidong Chen

Reputation: 450

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;

Upvotes: 3

Mani
Mani

Reputation: 17595

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

Upvotes: 5

codercat
codercat

Reputation: 23291

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.

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:

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:

- (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/

Upvotes: 2

FluffulousChimp
FluffulousChimp

Reputation: 9185

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:

char rssiString[19];
sprintf(rssiString, "%d", self.selectedBeacon.rssi);
UnitySendMessage("Foo", "RSSIValue", rssiString );

Upvotes: 0

Michał Banasiak
Michał Banasiak

Reputation: 960

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.

Upvotes: 15

Related Questions