【发布时间】:2019-06-11 08:19:45
【问题描述】:
我有一个包含几个条件的方法。第一个条件工作正常,不会引起任何问题。但是,第二个会导致应用崩溃。
- (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:]:范围 {1, 2} 超出
数据长度 2'
【问题讨论】:
标签: objective-c nsinteger nsrangeexception