Reputation: 967
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?
Upvotes: 0
Views: 1468
Reputation: 13296
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")
}
Upvotes: 1
Reputation: 17576
I would not use XOR for this problem; I do realize that they do provide the answer to your particular problem but in general I wouldn't use them for high level logic. Understandable > clever.
In general, massive if
statements rapidly become difficult to understand.
This is the solution that I came up with:
//Create a function which determines if all the Bool values in an array are true
func allTrue(booleanValues: [Bool]) -> Bool {
for bool in booleanValues {
if bool == false {
return false
}
}
return true
}
//And a function which determines if all Bool values are false
func allFalse(booleanValues: [Bool]) -> Bool {
for bool in booleanValues {
if bool == true {
return false
}
}
return true
}
//This is a function which determines if a UITextField is empty or not (this could also be written as a standalone function if you need it elsewhere).
//It takes in a UITextField! and returns a Bool
let isTextFieldEmpty: ((textField: UITextField!) -> Bool) = { (textfield) -> Bool in
return textfield.text?.isEmpty ?? true
}
//Create an array of all of the textfields - this makes it easy to modify if you add or remove a textfield. It's also easy to verify that you didn't miss one of your six textfields in a giant if statement
let textfields = [txtCardNumber, txtCardType, txtNameofCard, txtMonthExpriy, txtYearExpiry, txtPayment]
//Turns the array of textfields into an array of Bool values representing if they are empty; we then use our allTrue() and allFalse() functions.
let textFields = [txtCardNumber, txtCardType, txtNameofCard, txtMonthExpriy, txtYearExpiry, txtPayment]
let textFieldsAreEmpty = textFields.map(isTextFieldEmpty)
if allTrue(textFieldsAreEmpty) || allFalse(textFieldsAreEmpty) {
//Do the stuff
}
Upvotes: 0