0

Objective C を使用した文字列の解析に関して簡単な質問があります。現在、*A_1_2_3_4_~ を出力する形式があります。この文字列には、メッセージの開始 (*)、ヘッダー (A)、値 (1)、およびメッセージの終了 (~) があります。私は arduino 用にこのコードを書きましたが、客観的な c で正しく動作するようには見えません。関数が行うことは、各値を分離して分類することです。非常に効率的で頑丈です。*A_1_1_1_~ を送信すると、A が値 1, 1, 1 の何かに対するものであることが認識されます。とにかく、ここにコード:

//protocol for parsing
-(void)protocol:(NSString*)input type:(Boolean*)comType
{
    Byte place = 0, index, ID, average, setGet; //any value < 256
    int probeType, tempType;
    char Header;
    NSString *output; //used for parsing
    double probeValue, tempValue;

//    if(comType){}
//    //wire = ""; //reset wire
//    else
//        analog.text = @"";//serial = ""; //reset serial

    //if error in message example: A_1_1_A_1_A_A_A*A_1_1_8.27_1_15.67_3_~
    //use '*' as start of message character and grab from *A_1_1_8.27_1_15.67_3_~

    probeType = input.length; //get length of string

    index = [input rangeOfString:@"*" options:NSBackwardsSearch].location; //index = input.lastIndexOf("*"); //index contains start of message
    if(index < 0)  //not available or incorrect format
        return;
    input = [input substringFromIndex:index]; //remove incorrect stuff
    average = [input rangeOfString:@"~"].location; //input.indexOf("~", index); //grab end of message
    input = [input substringWithRange:NSMakeRange(index+1, average+index)]; //input = input.substring(index + 1, average + 1); //grab valid string only, lower bandwidth
    //    [self alert: input];
    for(Byte i = 0; i < 8; i++) //limit the message length as well
    {
        //        index = [input rangeOfString:@"_"].location; //grab location of
        index = [input rangeOfString:@"_" options:Nil range:NSMakeRange(place, input.length)].location;//index = input.indexOf("_", place); //gets first update
        //        [self alert:index];
        if(index < 0 || [input characterAtIndex:index+1] == '~')//input.substring(place, index + 1) == "~") //no '_' found or EOM
            break; //two birds one stone
        output = [input substringWithRange:NSMakeRange(place, index)];//output = input.substring(place, index); //grab in between
        //input = [input stringByReplacingCharactersInRange:NSMakeRange(0, index) withString:@""];//remove already read

        switch(i)
        {
            case 0: //header
                Header = [output characterAtIndex:0]; //fast conversion from String to char
                break;
            case 1: //ID
                ID = [output intValue]; //toInt() returns long...not int; perfect for conductivity
                break;
            case 2: //Probe Type
                probeType = [output intValue];
                break;
            case 3: //Probe Value
                probeValue = [output doubleValue];
                break;
            case 4: //Temperature Type
                tempType = [output intValue];
                break;
            case 5: //Temperature Value
                tempValue = [output doubleValue];
                break;
            case 6: //Average
                average = [output intValue];
                break;
            case 7:
                setGet = [output intValue];
                break;
        }
        place = index + 1;
        [self alert: output];
    }
}
4

2 に答える 2

1

形式がどのように見えるかについて、すべてのパラメーターが何であるかは明確ではありませんが、文字列から数値を抽出するには、このようにします。あなたの質問のように、文字列の最初の「*」を探し、最後から逆方向に検索します。

 - (void)viewDidLoad {

    [super viewDidLoad];
    NSCharacterSet *nonNumbers = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    NSString *s = @"A_1_1_A_1_A_A_A*A_1_1_8.27_1_15.67_3_~"; // test string
    NSInteger locationOfAsterisk = [s rangeOfString:@"*" options:NSBackwardsSearch].location;
    if (locationOfAsterisk != NSNotFound) {
        NSString *validString = [s substringFromIndex:locationOfAsterisk + 1];
        NSMutableArray *parsedArray = [[s componentsSeparatedByString:@"_"] mutableCopy];
        NSIndexSet *indxs = [parsedArray indexesOfObjectsPassingTest:^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
            return [obj rangeOfCharacterFromSet:nonNumbers].location != NSNotFound ;
        }];
        [parsedArray removeObjectsAtIndexes:indxs];
        [parsedArray insertObject:[validString substringToIndex:1] atIndex:0];
        NSLog(@"%@",parsedArray);

    }else{
        NSLog(@"Not a valid format");
    }
}

これは、最初の要素がアスタリスクに続く文字であり、その後にチルダまでのすべての数字が続く配列を返します。

于 2013-07-19T01:36:47.790 に答える