Silvia Camara
Silvia Camara

Reputation: 3

Swift - Command Failed Due to signal: Segmentation Fault 11

I've already looked extensively at other posts about seg faults in swift and I found one that pinpointed my problem but now my question is how do I fix it? I found the error to be my signIn function (Note: I haven't changed the name, it actually works as a SIGNUP function and creates users) and when I comment it out it gets rid of the problem. However, I kinda need it, so how do I fix it?

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class SignUpViewController: UIViewController {
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!
    @IBOutlet var usernameField: UITextField!

    var ref: DatabaseReference!
    override func viewDidLoad() {
        super.viewDidLoad()
        ref = Database.database().reference()
    }

    // Actually Sign Up button
    @IBAction func SignIn(_ sender: AnyObject) {
        Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                print("User created...")
                self.ref.child("UserProfile").child(user!.uid).setValue([
                    "username" : self.usernameField.text!
                    ])
            }
        })
    }
}

Upvotes: 0

Views: 45

Answers (1)

sschale
sschale

Reputation: 5188

We can't really help you without a full error log, however, you have many force unwraps (!). If any string is null your app will crash. Instead, you should conditionally unwrap:

if let email = emailField.text, let password = passwordField.text {
    Auth.auth().createUser(withEmail: email, password: password!
    .....
}

Same logic also goes for user.

Only force unwrap when you SURE you won't have nil. Surefire way to crash.

Upvotes: 1

Related Questions