我有一个包含一些条件的方法。第一个条件工作正常,不会引起任何问题。但是,第二个导致应用程序崩溃。
- (void)didReceiveGaiaGattResponse:(CSRGaiaGattCommand *)command
{
GaiaCommandType cmdType = [command getCommandId];
NSData *requestPayload = [command getPayload];
uint8_t success = 0;
NSLog(@"cmdType: %li", (long)cmdType);
[requestPayload getBytes:&success range:NSMakeRange(0, sizeof(uint8_t))];
if (cmdType == GaiaCommand_GetCurrentBatteryLevel && requestPayload.length > 1)
{
uint16_t value = 0;
[requestPayload getBytes:&value range:NSMakeRange(1, sizeof(uint16_t))];
NSInteger battery = CFSwapInt16BigToHost(value);
[self sendEventWithName:someDEVICE_BATTERY_CHANGED body:@{@"batteryLevel":[NSNumber numberWithInteger:battery]}];
return;
}
else if (cmdType == GaiaCommand_GET_FBC && requestPayload.length > 1)
{
uint16_t value = 0;
[requestPayload getBytes:&value range:NSMakeRange(1, sizeof(uint16_t))];
NSInteger feedbackCancellationMode = CFSwapInt16BigToHost(value);
[self sendEventWithName:FEEDBACK_CANCELLATION_MODE body:@{@"feedbackCancellationMode": [NSNumber numberWithInt:feedbackCancellationMode]}];
return;
}
//do more stuff
}
有条件的
if (cmdType == GaiaCommand_GetCurrentBatteryLevel && requestPayload.length > 1)
工作没有问题。
然而,有条件的
else if (cmdType == GaiaCommand_GET_FBC && requestPayload.length > 1)
在 xcode 中导致以下警告
隐式转换失去整数精度:“NSInteger”(又名“long”)到“int”
另外,我也在调试器中看到了错误信息
* 由于未捕获的异常 'NSRangeException' 终止应用程序,原因:'* -[_NSInlineData getBytes:range:]: range {1, 2} 超出
数据长度 2'