user198725878
user198725878

Reputation: 6386

The left operand of '>' is a garbage value

When i build and anylayse my app , i get the below memeory warning

"The left operand of '>' is a garbage value" at this line isTrue = (newLength > 20) ? NO : YES;

What is the problem over here.Thanks for any help

 (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength;
    BOOL isTrue;
    if(textField == txtFirstName){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue = (newLength > 20) ? NO : YES;
    }
    if(textField == txtLastName){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue = (newLength > 20) ? NO : YES;
    }
    if(textField == txtEmail){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 100) ? NO : YES;
    }
    if(textField == txtCompanyName){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    if(textField == txtPassword){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    if(textField == txtRe_EnterPassword){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    if(textField == txtZipCode){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 20) ? NO : YES;
    }
    if(textField == txtCountry){
        newLength = [textField.text length] + [string length] - range.length;
        //isTrue =  (newLength > 30) ? NO : YES;
    }
    isTrue = (newLength > 20) ? NO : YES;

    return isTrue;
}

Upvotes: 6

Views: 5449

Answers (5)

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

I have made some changes to your code I think now it would be work better.

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = 0;
    BOOL isTrue = false;

    if(textField == txtFirstName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue = (newLength > 20) ? NO : YES;
    }
    else if(textField == txtLastName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue = (newLength > 20) ? NO : YES;
    }
    else if(textField == txtEmail){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 100) ? NO : YES;
    }
    else if(textField == txtCompanyName){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtPassword){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtRe_EnterPassword){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else if(textField == txtZipCode){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 20) ? NO : YES;
    }
    else if(textField == txtCountry){
        newLength = [textField.text length] + [string length] - range.length;
        isTrue =  (newLength > 30) ? NO : YES;
    }
    else {
        isTrue = (newLength > 20) ? NO : YES;
    }

    return isTrue;
}

Upvotes: 1

Vikram Singh
Vikram Singh

Reputation: 1816

Try [a compare:b] returns -1 if a < b, 0 if a == b and 1 if a > b.

isTrue = ([newLength compare:20]==1) ? NO : YES;

Upvotes: 0

morningstar
morningstar

Reputation: 9142

Your code might be correct, if (at least) one of your if tests is always guaranteed to pass. But the analyzer cannot determine that. If you can convince yourself of that, you can ignore the warning.

But a better idea would be to change those into else ifs, and put one last else, inside which is an NSAssert.

Upvotes: 0

justin
justin

Reputation: 104698

the analyzer cannot confirm that your 'switch-like construct' does not 'fall through'. newLength's value may never be set if all those if cases fail. then in that event, it would qualify as a garbage value.

Upvotes: 1

Alex Deem
Alex Deem

Reputation: 4805

The problem occurs when all of the if conditions fail, ie. if textField is not one of the set you are expecting it to be.

In this case, newLength is never assigned to, and will be a random garbage value. Whilst this is probably "never going to happen", the static analyzer is not aware of that.

You should initialize newLength to have a default value, perhaps zero.

Upvotes: 16

Related Questions