Project Not Building- Xcode 10.1

I'm testing the login of my app using basic email and password authentication via Cloud Firestore.


Here's the code:

import UIKit
import Firebase

class LoginViewController: UIViewController {

    @IBOutlet weak var EmailTextField: UITextField!
    @IBOutlet weak var PasswordTextField: UITextField!
    
    var docRef: DocumentReference!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        docRef = Firestore.firestore().document("users/credentials")

        // Do any additional setup after loading the view.
    }
    
    @IBAction func SMLoginTapped(_ sender: Any) {
        guard let EmailText = EmailTextField.text, !EmailText.isEmpty else { return }
        guard PasswordText = PasswordTextField.text, !PasswordText.isEmpty else { return }
        let SaveData: [String: Any] = ["email": EmailText, "password": PasswordTextField]
        docRef.setData(SaveData) { (error) in
            if let error = error {
                print("Oh no! Got an error: \(error.localizedDescription)")
            } else {
                print("Data has been saved to Firestore!")
            }
        }
    }
        print("Login button tapped!")
        self.performSegue(withIdentifier: "SecondViewSegue", sender: self)
    }
    
    /*
    // 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.
    }
    */


I'm getting the following errors:

line 20: Use of unresolved identifier 'PasswordText"; did you mean 'PasswordTextField'?

line 20: Replace 'PasswordText' with 'PasswordTextField'



When I do the recommended debugging above, I still get errors. Could someone help me find the issue here?

You forgot the let :


guard let PasswordText = PasswordTextField.text, !PasswordText.isEmpty else { return }


In addition note that instances should start with lowerCap, by Swift conventions


    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!



        guard let emailText = EmailTextField.text, !EmailText.isEmpty else { return } 
        guard let passwordText = PasswordTextField.text, !PasswordText.isEmpty else { return }

Thanks I fixed that and also found that I was trying to assign the textField object to the password inside dictionary. Those corrections have eliminated the errors but the IBAction isn't working. It recognizes the tap (gives the "login button tapped" message) but doesn't actually login the user and go to the next segue.


(EDIT): I've taken a new approach


Here's what I have now:


import UIKit
import Firebase


class SignUpViewController: UIViewController {
    
    //Outlets
    @IBOutlet weak var firstNameText: UITextField!
    @IBOutlet weak var lastNameText: UITextField!
    @IBOutlet weak var emailText: UITextField!
    @IBOutlet weak var passwordText: UITextField!
    @IBOutlet weak var signUpButton: UIButton!
    

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func signUpButtonTapped(_ sender: Any) {
        guard let firstName = firstNameText.text,
        let lastName = lastNameText.text,
        let email = emailText.text,
        let password = passwordText.text else { return }
        
        Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
            if let error = error {
                debugPrint("Error creating user: \(error.localizedDescription)")
            }
            let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
            changeRequest?.displayName = firstName
            changeRequest?.commitChanges(completion: { (error) in
                if let error = error {
                    debugPrint(error.localizedDescription)
                }
        })
            guard let userId = authResult?.user else { return }
            Firestore.firestore().collection(USERS_REF).document(userId).setData([
                USERNAME : firstName,
                DATE_CREATED : FieldValue.serverTimestamp()
                ], completion: { (error) in
                    
                    if let error = error {
                        debugPrint(error.localizedDescription)
                    } else {
                        self.dismiss(animated: true, completion: nil)
                    }
            })
        }
    }
    
    

}


After fixing the previous errors, I get a new error:

Cannot invoke 'setData' with an argument list of type '([String : Any], completion: ((Error?) -> Void)?)' (line 37)



I'm not sure why it's doing this. Any idea why I'm getting this error?

You pass incorrect parameters types to setData.


Are you sure of the syntax for setData ? Where did you find the code sample ?


In documentation,

h ttps://firebase.google.com/docs/reference/ios/firebasefirestore/api/reference/Classes/FIRDocumentReference


for OBJECTIVE-C

- (void)setData:(nonnull NSDictionary<NSString *, id> *)documentData

completion:(nullable void (^)(NSError *_Nullable))completion;

Parameters

documentData: An NSDictionary containing the fields that make up the document to be written.

completion: A block to execute once the document has been successfully written to the server. This block will not be called while the client is offline, though local changes will be visible immediately.


for Swift:

h ttps://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/DocumentReference#setdata_completion

func setData(_ documentData: [String : Any], completion: ((Error?) -> Void)? = nil)

Parameters

documentData: An NSDictionary containing the fields that make up the document to be written.

completion: A block to execute once the document has been successfully written to the server. This block will not be called while the client is offline, though local changes will be visible immediately.


Parameters are String ; are USERNAME and DATE_CREATED String ?


If they are String, I would try

           Firestore.firestore().collection(USERS_REF).document(userId).setData([
                USERNAME : firstName as Any,                           // TRY THIS
                DATE_CREATED : FieldValue.serverTimestamp() as Any     // TRY THIS
                ], completion: { (error) in
                    if let error = error {
                        debugPrint(error.localizedDescription)
                    } else {
                        self.dismiss(animated: true, completion: nil)
                    }
            })


If not, I would try

           Firestore.firestore().collection(USERS_REF).document(userId).setData([
                "USERNAME" : firstName,                                            // or firstName as Any
                "DATE_CREATED" : FieldValue.serverTimestamp()      // or FieldValue.serverTimestamp() as Any
                ], completion: { (error) in
                    if let error = error {
                        debugPrint(error.localizedDescription)
                    } else {
                        self.dismiss(animated: true, completion: nil)
                    }
            })

Apparently I had set the data incorrectly. It builds now, reading:


guard let userId = authResult?.user else { return }
            let userData: [String: Any] = [
                "firstName" : "",
                "User ID" : userId,
                "dateCreated" : FieldValue.serverTimestamp(),
                ]
            let db = Firestore.firestore()
            db.collection("users").document("one").setData(userData) { err in
                if let err = err {
                    print("Error writing document: \(err)")
                } else {
                    print("Document successfully written!")
                }
            }
        }
    }
}


But, it crashes and gives a runtime error:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { //error: 
//libc++abi.dylib: terminating with uncaught exception of type NSException
//(lldb)

//3 comments above all one line



Do you know what this crash means?

>Do you know what this crash means?


Whenever you get an error, you should first search on it for previous discussion. If you've done that and don't understand what it means or next action, then ask, being sure to provide a detailed explanation.


Also note that if this is all new to you, you might want to use the 'Getting Started' forum so others don't assume too much in the way of your current skills.


Pls. see For Best Results - Read the Label

Project Not Building- Xcode 10.1
 
 
Q