I am doing a signup module which consists of 3 different pages. 1st page personal details , 2nd page login details and 3rd page payment details. While signup the 1st and 2nd pages are mandatory but the 3rd page is not. the 3rd page has 6 textfields. However the 3rd page has a validation that the user must enter all the values or must leave all the fields empty. to do this a logical XOR operation is necessary for me which i can't find out how to implement. help me. i now use the following code which is faulty
if((txtCardNumber.text!.isEmpty && txtCardType.text!.isEmpty && txtNameofCard.text!.isEmpty && txtMonthExpriy.text!.isEmpty && txtYearExpiry.text!.isEmpty && txtPayment.text!.isEmpty) == false )
i am using Swift 2.0 xcode 7.1.1. so ^
doesn't work
could any one give a logical solution or alternative solution for this?
You can do this without requiring an xor operator. This should be pretty simple to do if all your textfields are in an array. You can either create it in code or from your storyboard/xib using IBOutletCollection
.
This code gets all the Bools
from calling isEmpty
on the textfields and puts them into a Set
. If they are all true or all false, the set will only contain one value, if it's a mix of true/false then it will have two values.
let textFields: [UITextField] = ... // an array of the textfields to check
let emptyValues = Set(textFields.map { $0.text?.isEmpty ?? true })
if emptyValues.count == 1 {
print("All textfields are full or empty")
}