NSScanner仅使用非初始化变量。为什么?

时间:2013-10-04 09:38:53

标签: objective-c cocoa nsscanner

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

代码解析代表分子的字符串,例如@“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返回它们最初初始化的值。我错过了一些基本的东西吗?

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

0 个答案:

没有答案