Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top