将包含在字符串中的数字连接到另一个字符串

时间:2014-07-15 20:40:52

标签: string cocoa casting

我的Mac应用程序读取连接到串行端口的设备。读取返回4个字符,一次一个为';' '0''4''5'例如(总是一个分号后跟3个整数。)我需要将它们连接成一个字符串以便在标签上显示。我尝试使用accumString = [accumString stringByAppendingString:(string)];导致崩溃。我认为这是因为数字不是字符串,但我不知道它们是什么,因为它们以字符串形式出现。

stringWithFormat也没有运气。

我的问题是: 1)如何确定数字的类型? 2)如何模拟测试输入?我无法直接访问该硬件。 3)如何将它们全部放在一起展示。

控制台日志的一部分:

2014-07-14 18:09:04.354 LRotor[357:707] Building return: ;
2014-07-14 18:09:04.356 LRotor[357:707] Building return: ;0
2014-07-14 18:09:04.358 LRotor[357:707] -[__NSMallocBlock__ stringByAppendingString:]: unrecognized selector sent to instance 0x60000005a250
2014-07-14 18:09:04.359 LRotor[357:707] An uncaught exception was raised
2014-07-14 18:09:04.360 LRotor[357:707] -[__NSMallocBlock__ stringByAppendingString:]: unrecognized selector sent to instance 0x60000005a250
2014-07-14 18:09:04.360 LRotor[357:707] (

    - (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data
{
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (rotorSelected == 2)
    {
      if ([string length] < 4)
        {
            NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            if ([string rangeOfCharacterFromSet:notDigits].location == NSNotFound)
            {
                accumString = [NSString stringWithFormat:@"%??", string];

            }
            else
            {
                accumString = [accumString stringByAppendingString:(string)];
            }
            NSLog(@"Building return: %@", accumString);
        }
        if ([accumString length] == 4)
        {
            string = accumString;
        }

解决问题的修订代码。

- (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data
{
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Received: %@", string);
    if (rotorSelected == 2)
    {
        if ([string length] < 4)
        {
          if (!accumString)
          {
            accumString = [[NSMutableString alloc] init];
          }
          [self processPartialString:string];
        }
    }
}

- (void)processPartialString:(NSString *)iString
{
    NSString *tmp = @"";
    NSInteger x;    

    NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    if ([iString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
    {
        x = [iString integerValue];
        tmp = [NSString stringWithFormat:@"%ld",(long) x];
        [accumString appendString:(tmp)];
        NSLog(@"Building return: %@", accumString);
    }
    else
    {
        [accumString appendString:(iString)];
        NSLog(@"Building return: %@", accumString);
    }
    if ([accumString length] > 3)
    {
        iString = [accumString substringFromIndex:1];
        NSLog(@"Showing label: %@", iString);
        [labelCurrHdg setStringValue:iString];
        [accumString setString:@""];
    }
}

- (void)startTimer
{
    hdgTimer = [NSTimer scheduledTimerWithTimeInterval:0.6
                                                target:self
                                              selector:@selector(printHdg)
                                              userInfo:nil
                                               repeats:YES];
}

- (void)printHdg
{
    NSInteger currH = [labelCurrHdg intValue];
    NSInteger wantedH = [hdgVal intValue];
    NSData *dataToSend;
    NSString *str;
    if (currH > wantedH - 3 && currH  < wantedH + 3)
    {
        [hdgTimer invalidate];
        hdgTimer = nil;
        [labelCurrHdg setStringValue:hdgVal];
        [labelCurrHdg needsDisplay];
        return;
    }

  switch(rotorSelected)
    {
        case 2:
            str = @"AI1;";
            break;
        case 7:
            str = @"C\r";
            break;
        default:
            str = @"";
            break;
    }
    if ([str length] >0)
    {
        dataToSend = [str dataUsingEncoding:NSUTF8StringEncoding];
        [serialPort sendData:dataToSend];
    }
}

0 个答案:

没有答案