-5

binary operator '&' cannot be applied to two bool operands

Here I'm sharing my code.

@IBOutlet weak var fnameTxt: UITextField!
@IBOutlet weak var lnameTxt: UITextField!
@IBOutlet weak var addrsTxt: UITextField!

if (fnameTxt.validate()) & (lnameTxt.validate()) & (addrsTxt.validate()){
 print("successfully validated")
}else{
 print("validation failed")
}

-(BOOL)validate{
    if(isMandatory){
        if([self.text length]==0){
            [self showErrorIconForMsg:strLengthValidationMsg];
            return NO;
        }
    }
    for (int i=0; i<[arrRegx count]; i++) {
        NSDictionary *dic=[arrRegx objectAtIndex:i];
        if([dic objectForKey:@"confirm"]){
            TextFieldValidator *txtConfirm=[dic objectForKey:@"confirm"];
            if(![txtConfirm.text isEqualToString:self.text]){
                [self showErrorIconForMsg:[dic objectForKey:@"msg"]];
                return NO;
            }
        }else if(![[dic objectForKey:@"regx"] isEqualToString:@""] && [self.text length]!=0 && ![self validateString:self.text withRegex:[dic objectForKey:@"regx"]]){
            [self showErrorIconForMsg:[dic objectForKey:@"msg"]];
            return NO;
        }
    }
    self.rightView=nil;
    return YES;
}

Here, validate() returns boolean value,any idea on this??

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Nagaraju
  • 1
  • 2

3 Answers3

2

A single ampersand ('&') is for doing bitwise operations. From the question that you have asked it seems you intended to use the logical AND operator ('&&'). So replace & with && like

if (fnameTxt.validate()) && (lnameTxt.validate()) && (addrsTxt.validate()){
 print("successfully validated")
}else{
 print("validation failed")
}
Aravind A R
  • 2,576
  • 1
  • 10
  • 23
  • Good explanation. – Hemang Sep 21 '17 at 07:18
  • @Aravind, I tried with && also, when I use &&, it validating one after other. i.e, 'fnameTxt' is validated then only it checking the 'lnameTxt'. I want to validate 3 at a time.. – Nagaraju Sep 21 '17 at 08:19
  • @Nagaraju yup when you use && it will be validated one after the other, I don't think there is any operation that could allow you to validate all the 3 at a time. – Aravind A R Sep 21 '17 at 08:49
1

Pleas use && instead &. i.e,

if ((fnameTxt.validate()) && (lnameTxt.validate()) && (addrsTxt.validate())){
 print("successfully validated")
}else{
 print("validation failed")
}
Anbu.Karthik
  • 77,564
  • 21
  • 153
  • 132
dmp
  • 66
  • 9
  • Hi Karthik, when I use &&, it validating one after other. i.e, 'fnameTxt' is validated then only it checking the 'lnameTxt'. I want to validate 3 at a time.. – Nagaraju Sep 21 '17 at 08:06
  • We cannot simultaneously do all the validation stuff together. I think you have three textfields and a button. If you enable the button after validating all the three textfields(means all the fields are mandatory), then the above code is enough. If you show any error message or red border around the textfield to inform the user about the validation. Then you can use textfields textchange events and put the desired code there(please see the link https://stackoverflow.com/questions/28394933/how-do-i-check-when-a-uitextfield-changes) – dmp Sep 21 '17 at 09:21
1

You can't use bitwise operators to compare two operands, if you want to compare, you should use logical operators. So instead of & use &&.

Also, I would like to suggest you:

As you have a common validate() function for all of your UITextFields I would suggest you make a function signature like this.

class func validates(textFields: Array<UITextField>) -> Bool {
    //Return true or false based on the validation.
    textFields.forEach { (textField) in {
        //do something with each of the textField in textFields array.
    }
}

In that way, you can simply, check like this

if UITextField.validates([fnameTxt, lnameTxt, addrsTxt]) {
    //Go ahead!
} else {
   //Handle the invalidation.
}

Note: As validates() is a class function you can directly access it with the class name.

Hemang
  • 25,740
  • 17
  • 113
  • 171