6

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?

5
  • 2
    use stringWithFormat: Commented Feb 13, 2014 at 11:45
  • What type is self.selectedBeacon.rssi? Commented Feb 13, 2014 at 11:47
  • try NSString *rssiString = [NSString stringWithFormat:@"%d", (int)self.selectedBeacon.rssi] ; Commented Feb 13, 2014 at 11:48
  • NSString *rssiString = (int)self.selectedBeacon.rssi;. - Really!, Do you have any idea of what pointers mean & how type casting in objective-c works, if not than go and read some tutorials. Commented Feb 13, 2014 at 12:40
  • the modern answer ... stackoverflow.com/a/21258535/294884 Commented Oct 14, 2014 at 12:22

5 Answers 5

15
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.

Sign up to request clarification or add additional context in comments.

3 Comments

If it's a CLBeacon, then RSSI is defined that way: @property (readonly, nonatomic) NSInteger rssi;. So you code should work (but may put a warning on the format done).
you're right. warning will appear on 64-bit environment where NSInteger is typedef to long. Then he has to use %ld format specifier.
@MichałBanasiak Per the documentation, the proper thing to do with NSInteger is always to use %ld and cast to long. This way, your code will do the correct thing no matter what platform it is run on.
5

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

Comments

3

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;

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.