UISwitch Submit to Firebase


I’ve been using the tutorial to create accounts using firebase authentication. So far, so good. I am having an issue getting the production settings set, so I can actually submit to the DB, but that’s not what is frustrating me.

Here’s what I am looking to do:
I’d like to allow a user to select from 8 UISwitches during sign-up that identify allergies the person might have. I’d like to then understand what they selected and submit that when submitting their first name, last name like in the tutorial.

Any help would be greatly appreciated. I’ve been stuck for a couple of days and stack overflow doesn’t seem to be saving me this time.


Here’s what I’ve written:

import UIKit
import FirebaseAuth
import Firebase

class SignUpViewController: UIViewController {

@IBOutlet weak var firstNameTextField: UITextField!

@IBOutlet weak var lastNameTextField: UITextField!

@IBOutlet weak var usernameTextField: UITextField!

@IBOutlet weak var emailTextField: UITextField!

@IBOutlet weak var passwordTextField: UITextField!

@IBOutlet weak var milkSwitch: UISwitch!

@IBOutlet weak var peanutSwitch: UISwitch!

@IBOutlet weak var wheatSwitch: UISwitch!

@IBOutlet weak var treeNutSwitch: UISwitch!

@IBOutlet weak var fishSwitch: UISwitch!

@IBOutlet weak var shellFishSwitch: UISwitch!

@IBOutlet weak var eggSwitch: UISwitch!

@IBOutlet weak var soySwitch: UISwitch!

@IBOutlet weak var addAFamilyButton: UIButton!

@IBOutlet weak var signUpButton: UIButton!

@IBOutlet weak var errorLabel: UILabel!

override func viewDidLoad() {

    // Do any additional setup after loading the view.
func setUpElements (){
    errorLabel.alpha = 0
func showError(_ message:String) {
    errorLabel.text = message
    errorLabel.alpha = 1
func transitionToHome (){
    let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
    view.window?.rootViewController = homeViewController
//Check the fields and validate that data is correct.
func validateFields() -> String? {

    //Check that all fields are filled in
    if firstNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        lastNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        usernameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
        return "Please fill in all fields."
    //Check if password is secure
    let cleanedPassword = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    if Utilities.isPasswordValid(cleanedPassword) == false {
        // Password isn't secure enough
        return "Please make sure your password is at least 8 characters, contains a special character and a number"
    return nil

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.

@IBAction func signUpTapped(_ sender: Any) {
    // Validate the fields
    let error = validateFields()
    if error != nil {
        // There's something wrong with the fields, show error message
    else {
    // Cleaned Versions of the data
        let firstname = firstNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let lastname = lastNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let username = usernameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let milk = milkSwitch
        let peanut = peanutSwitch
        let wheat = wheatSwitch
        let treenut = treeNutSwitch
        let fish = fishSwitch
        let shellfish = shellFishSwitch
        let egg = eggSwitch
        let soy = soySwitch
    // Create the user
        Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
            // Check for errors
            if err != nil {
                //There was an error creating the user
                self.showError("Error creating user")
            else {
                // User was created successfully
                let db = Firestore.firestore()
                db.collection("users").addDocument(data: ["firstname":firstname, "lastname":lastname, "username":username, "uid": result!.user.uid, "milk":self.milkSwitch.isOn, "peanut":self.peanutSwitch.isOn, "wheat":self.wheatSwitch.isOn, "treenut":self.treeNutSwitch.isOn, "fish":self.fishSwitch.isOn, "shellfish":self.shellFishSwitch.isOn, "egg":self.eggSwitch.isOn, "soy":self.soySwitch.isOn ]) { (error) in
                    if error != nil {
                        //Show error message
                        self.showError("Your allergies have not been saved. Please login to your account")
                // Transiition to the home screen
                self.transitionToHome ()


you can use the “.isOn” of the UISwitch so you can know if the switch as on or not