简体   繁体   English

为什么此条件导致错误?

[英]Why does this conditional cause an error?

I have a method that contains a few conditionals. 我有一个包含一些条件的方法。 The first conditional works fine and does not cause any problems. 第一个条件工作正常,不会引起任何问题。 However, the second one causes the app to crash. 但是,第二个导致应用程序崩溃。

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

The conditional 有条件的

if (cmdType == GaiaCommand_GetCurrentBatteryLevel && requestPayload.length > 1) 如果(cmdType == GaiaCommand_GetCurrentBatteryLevel && requestPayload.length> 1)

works without problems. 没有问题的作品。

However, the conditional 但是,有条件的

else if (cmdType == GaiaCommand_GET_FBC && requestPayload.length > 1) 否则(cmdType == GaiaCommand_GET_FBC && requestPayload.length> 1)

causes the following warning in xcode 在xcode中导致以下警告

Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int' 隐式转换将失去整数精度:将'NSInteger'(aka'long')转换为'int'

In addition, I also saw the error message in the debugger 此外,我还在调试器中看到了错误消息

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_NSInlineData getBytes:range:]: range {1, 2} exceeds *由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[_ NSInlineData getBytes:range:]:范围{1、2}超出

data length 2' 数据长度2'

Consider what this is telling you: 考虑一下这是在告诉你什么:

Terminating app due to uncaught exception 'NSRangeException', reason:
'-[_NSInlineData getBytes:range:]: range {1, 2} exceeds data length 2'

Your data object is 2 bytes in length. 您的数据对象的长度为2个字节。 The first byte, at position 0, is (according to your code) the success value. 第一个字节位于位置0,是(根据您的代码) success值。 That leaves one more byte at position 1 to handle. 这样在位置1还有一个字节要处理。 But your code is attempt to copy 2 bytes out of it — that's the range {1, 2} in the message; 但是您的代码尝试从其中复制2个字节-这就是消息中的range {1, 2} a range starting at position 1 and with a length of 2. You're reading past the end of the data. 一个从位置1开始且长度为2的范围。您正在读取数据的末尾。

You have to check that the data has enough data to satisfy the -getBytes:... call you're attempting to make. 您必须检查数据是否有足够的数据来满足您尝试进行的-getBytes:...调用。 You may also need to correct your assumptions about how large the cancellation mode value in the buffer is supposed to be, because it's apparently smaller than you expect. 您可能还需要更正关于缓冲区中取消模式值应该有多大的假设,因为它显然比您期望的要小。 Your code assumes it's a uint16_t (2 bytes) but there's only one byte left in the data. 您的代码假定它是一个uint16_t (2个字节),但是数据中只剩下一个字节。

[NSNumber numberWithInt:feedbackCancellationMode]}]

应该

[NSNumber numberWithInteger: feedbackCancellationMode]}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM