문제

I have a validation method by which I analyse whether a particular variable passes a validation criteria.

Here's the code:

-(BOOL)validateFields{
    BOOL valid = FALSE;
    if (dateEntry != TRUE && saveOrderType != TRUE) {
        if (_editRequired==YES) {
            if ([[[editedTextField text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""]) {
                valid = FALSE;
            } else {
                valid = TRUE;
            }
        } else {
            valid = TRUE;
        }
        if (_editRegEx) {
            NSRegularExpression *regex = [NSRegularExpression
                                      regularExpressionWithPattern:_editRegEx
                                      options:NSRegularExpressionCaseInsensitive
                                      error:nil];
            if ([regex numberOfMatchesInString:[editedTextField text] options:0 range:NSMakeRange(0, [[editedTextField text] length])]==0) {
                valid=FALSE;
            } else {
                valid = TRUE;
            }
        } else {
            valid = TRUE;
        }

    } else {
        valid = TRUE;
    }
    return valid;
}

I'm getting 3 instances of Value stored to 'valid' is never read which is strange because eventually, it's returned at the end of the method.

I'm getting it on the first three instances of setting the variable, only on these three:

if (_editRequired==YES) {
    if (//checks if the field contains any characters) {
        valid = FALSE;
    } else {
        valid = TRUE;
    }
} else {
     valid = TRUE;
}

Can anybody help here?

도움이 되었습니까?

해결책

Your if (_editRequired==YES) {} conditional is completely overridden by the if (_editRegEx) {} conditional, so none of the assignments inside the former are ever used: valid is always reassigned in the second conditional.

다른 팁

You have this warning because you store an other value after this if/else. Indeed just after that you have an other if (to test _editRegEx) and store a value in all cases (if and else). So your first if is not needed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top