Dictionary Scope

I have a dictionary that I am trying to append information to but instead of appending to it just deletes the previous information. The dictionaries are in two different scopes. The dictionary name is personal info. They are on line 40 and 47. I tried doing personalInfo.append but it tells me that it has no member append. I am trying to save information to a database from to different views. This is the code that I have right now:


  @IBAction func sendPhoneAuthCode(_ sender: Any){
        if let phoneNum = phoneNumber.text {    // No more need to unwrap phoneNum later
            
            
            PhoneAuthProvider.provider().verifyPhoneNumber(phoneNum, uiDelegate: nil) { (verificationID, error) in
                
                // Sign in using the verificationID and the code sent to the user
                // …
                //  if phoneNum == nil {
                if error != nil {
                    print("verifyPhoneNumber went NOT OK")
                    // REPLACE BY print       self.present(alert, animated: true, completion: nil)
                    
                    
                    DispatchQueue.global(qos: .userInitiated).async {
                        let titre = " Missing Information "
                        let detailMsg = "Please enter your phone number"
                        let controller = UIAlertController(title: titre, message: detailMsg, preferredStyle: .alert)
                        
                        let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel)
                        controller.addAction(okAction)
                        DispatchQueue.main.async {
                            self.present(controller, animated: true, completion: nil)
                        }
                        
                    }
                    
                    
                    return
                } else {
                    print("verifyPhoneNumber went OK")
                    // You have to test credential here, where do you do this ?
                }
            }
        } else {
            print("phoneNumber.text is nil")
            return
        }
        personalInfo = ["Phone Number" : phoneNumber.text]
        print("Setting userDefaults", verificationID)
        UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
    }


 @IBAction func savePersonalInfo(_ sender: Any) {
        personalInfo = ["First Name" : firstName.text, "Last Name" : lastName.text, "Email" : email.text, "Address Line 1" : addressLine1.text, "Address Line 2" : addressLine2.text]
        let ref = Database.database().reference()
        ref.childByAutoId().setValue(personalInfo)
    }

Accepted Reply

When you write a question of Dictionary, you should better include how you declared the variable, to get better answer sooner.


I assume:

var personalInfo: [String: Any] = [:]

If you declaration is something far from this, you may need to modify the following code.


And, when you want to modify or add some limited entries of a Dictionary and keep all other entries as is, you just need to use the subscript notation:

        personalInfo["Phone Number"] = phoneNumber.text ?? ""

or:

        personalInfo["Phone Number"] = phoneNumber.text ?? ""
        personalInfo["First Name"] = firstName.text ?? ""
        personalInfo["Last Name"] = lastName.text ?? ""
        personalInfo["Email"] = email.text ?? ""
        personalInfo["Address Line 1"] = addressLine1.text ?? ""
        personalInfo["Address Line 2"] = addressLine2.text ?? ""


Or else you can use `merge` method in your second case if you prefer:

        personalInfo.merge([
            "First Name" : firstName.text ?? "",
            "Last Name" : lastName.text ?? "",
            "Email" : email.text ?? "",
            "Address Line 1" : addressLine1.text ?? "",
            "Address Line 2" : addressLine2.text ?? ""] as [String: Any]
            , uniquingKeysWith: {_, new in return new})


You can omit `?? ""` in some cases.


And this is not the main topic of your question, but the lines 39.-41. should be in the completion handler of `verifyPhoneNumber` method.

Replies

Getting better, but not showing enough info to solve your issue.

Have you ever read other thread in the forums? I mean other than yours. Some questions have only one answer, which is the right answer.

That happens because the question contained enough info to solve the issue.


You are still ignoring many of my question marks. But once you have shown your full (I hope you are not hiding any more) code, the most important thing might be these:


Do you never push any of the buttons in the screen nor manipulate any other UI controls?

If you do, please write it or them in order.

Do you never check the displayed contents in the text field or any other thing of the screen?

If you do, please write them in addition to when.


To check something is going wong or not, what do you do? Which button do you press? And what do you get? And what do you expect there?


And the segue "passwordAuthLoggedIn", where does it go?