Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift multiple conditions if statement with && operation [duplicate]

I have 4 text boxes.I don't want to allow user to let all these 4 textfields empty. How can I check multiple conditions in swift. I did like this but it's giving me an error

 if self.txtIncomeAd.text?.isEmpty && self.txtIncomeRec.text?.isEmpty &&

like wise. What is the correct way I can do this? Please help me. Thanks

like image 429
user1960169 Avatar asked Jun 09 '16 07:06

user1960169


People also ask

How do you write an if statement with multiple conditions?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Can Switch case have multiple conditions Swift?

Unlike C, Swift allows multiple switch cases to consider the same value or values. In fact, the point (0, 0) could match all four of the cases in this example. However, if multiple matches are possible, the first matching case is always used.

What is #if in Swift?

Swift if Statement The if statement evaluates condition inside the parenthesis () . If condition is evaluated to true , the code inside the body of if is executed. If condition is evaluated to false , the code inside the body of if is skipped.


1 Answers

you can simply use isEmpty property.

if !self.textFieldOne.text!.isEmpty && !self.textFieldTwo.text!.isEmpty && !self.textFieldThree.text!.isEmpty && !self.textFieldFour.text!.isEmpty {
 ...

}

or you can also safely unwrap the text value and the check that it is empty of not

if let text1 = self.textFieldOne.text, text2 = self.textFieldTwo.text, text3 = self.textFieldthree.text,text4 = self.textFieldFour.text where !text1.isEmpty && !text2.isEmpty && !text3.isEmpty && !text4.isEmpty {
  ...
  }

Or you can compare with Empty "" String

if self.textFieldOne.text != "" && self.textFieldTwo.text != "" && self.textFieldThree.text != "" && self.textFieldFour.text != ""   {
  ...
}

and we can also do this with Guard

guard let text = self.myTextField.text  where !text.isEmpty else {
  return
}
like image 65
Sahil Avatar answered Sep 29 '22 11:09

Sahil