Basically, my problem is a signed char to int and string conversion in cocoa.
I found this piece of code in an open source cocoa bluetooth application and am trying to apply it to my own.
Basically, I get a signed char output from the variable “RSSI”, and want to convert it to an int and a string, the string for outputting to the log and the int for further calculation. However, no matter what I try, I cannot seem to get it converted, and just get an EXEC_BAD_ACCESS if I try outputting the signed char to the log as it is.
A typical value for the signed char would be ” -57 ‘\307’ ” quoted directly from the process before it is held up by the NSLog. Here’s the code:
- (BOOL)isInRange {
BluetoothHCIRSSIValue RSSI = 127; /* Valid Range: -127 to +20 */
if (device) {
if (![device isConnected]) {
[device openConnection];
}
if ([device isConnected]) {
RSSI = [device rawRSSI];
[device closeConnection];
NSLog(RSSI);
}
}
return (RSSI >= -60 && RSSI <= 20);
}
Thanks in advance.
NSLog()takes anNSStringformat string as its first argument, and an (optional) variable length list of variables for the format specifiers in the format string after that:NSLog(@"RSSI: %c", RSSI);What you’ve got now (
NSLog(RSSI);) is simply wrong. It should be giving you compiler warnings like these:You should always pay attention to compiler warnings, not ignore them. Especially when your program crashes on the same line the warnings refer to, they should be a red flag to you that you’ve made a mistake.
As an aside, I should mention that
NSLog()works very much likeprintf(). The two major differences are thatNSLog‘s format string should be an Objective-C string literal (@"string"), not a standard C char string ("string"), and that the format specifier for an object is%@.%@is replaced by the string returned by calling the-descriptionmethod on the object to be printed.