0

以下代码有效,但会产生有关未初始化变量的警告。

代码解析代表分子的字符串,例如@"2C12;5H1;1O16"。它检查它的格式是否正确,并且所有组件都是有效的同位素。如所列,它运行良好,但会从编译器产生警告。

//listOfNames is a global variable, an NSArray containing strings of all isotope names i.e. @"Na23"

-(bool)canGrokSpeciesName:(NSString*)theSpeciesName{
 //Test that the species name makes sense
 //Names should be of the form of repeated @"nnSSaa" with nn being the multiplier, SS being the element name, aa being the atomic mass number

bool couldGrok = [theSpeciesName isNotEqualTo:@""];
if(couldGrok){

    NSArray* listOfAtomsWithMultiplier = [theSpeciesName componentsSeparatedByString:@";"];

    for (int i=0; i<[listOfAtomsWithMultiplier count]; i++) NSLog(@"%@", [listOfAtomsWithMultiplier objectAtIndex:i]);

    NSInteger multiplet[ [listOfAtomsWithMultiplier count] ];
    bool ok[ [listOfAtomsWithMultiplier count] ];

    bool noFail = true;
    for (int i=0; i<[listOfAtomsWithMultiplier count]; i++) {
        NSScanner* theScanner = [NSScanner scannerWithString:[listOfAtomsWithMultiplier objectAtIndex:i]];
        NSString* multipletString;

        if(![theScanner isAtEnd]) [theScanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&multipletString];
        multiplet[i] = [multipletString integerValue];

        NSString* atomString;
        if(![theScanner isAtEnd]) [theScanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&atomString];

        NSInteger A;
        if(![theScanner isAtEnd]) [theScanner scanInteger:&A];

        NSLog(@"%@%i with multiplet of %i", atomString, (int)A, (int)multiplet[i]);
        bool hasBeenFound = false;
        for(int j=0; j<[listOfNames count]; j++){
            if(noFail) hasBeenFound = hasBeenFound || [[listOfNames objectAtIndex:j] isEqualToString:[NSString stringWithFormat:@"%@%i", atomString, (int)A]];
        }
        couldGrok = couldGrok && noFail && hasBeenFound;
    }
}

return couldGrok;

}

但是,如果我初始化

NSString* multipletString = @"";
NSString* atomString = @"";
NSInteger A = -1;

然后 NSScanner 用它们最初的初始化值返回它们。我错过了一些基本的东西吗?

我不想忽略来自编译器的警告,但遵循编译器建议会破坏代码。所以,我想这更像是对基本信息的请求,而不是对如何修复我的代码的请求。谢谢!

4

0 回答 0