user1960169 user1960169 - 6 months ago 16
Swift Question

swift multiple conditions if statement with && operation

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

Answer

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 != ""   {
  ...
}