I want to implement

id? strong>,name,screen_name fromresult? .additionalUserInfo? .profilereturned when Twitter login with Firebase was successfuland save it to Firestore.
Useuidobtained fromAuth.auth (). CurrentUser!as the document name.


When the following code is executed, the following error occurs in the setData part and crashes.
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
I thought thatuIDhas becomenil, but I checked the value withprintjust before writing, but the value entered properly It was.
There were no other errors.
I checked it, but I could only find a pattern where the outlet connection of the label was not made, and it seemed that there was not much relation to this case, so I could not find a solution.
I'd be happy if you could teach me a solution.

Data structure I want to write
users {
    uID {
Currently described code (2019/11/29 last update)
var provider: OAuthProvider?
    var db: Firestore!
    func signInWithTwitter () {
        provider! .getCredentialWith (nil) {credential, error in
          if error! = nil {
            // Handle error
            print ("error")
            if credential! = nil {
                Auth.auth (). SignIn (with: credential!) {(Result, error) in
                if error! = nil {
                // Login error
                print ("error")
                // unwrap access token/secret token
                let credential = result? .credential as? OAuthCredential
                let accessToken = credential? .accessToken
                let secretToken = credential? .secret
                // Save Twitter access token/secret token values
                UserDefaults.standard.set (accessToken, forKey: "twitterAccessToken")
                UserDefaults.standard.set (secretToken, forKey: "twitterSecretToken")
                // Build user-specific DB
                let user = Auth.auth (). currentUser!
                let uID = user.uid
                let userInfo = result? .additionalUserInfo? .profile
                let twtrID = userInfo! ["id"] as! Int
                let displayName = userInfo! ["name"] as! String
                let screenName = userInfo! ["screen_name"] as! String
                self.db.collection ("users"). document (uID) .setData ([
                    "ID": twtrID,
                    "displayName": displayName,
                    "screenName": screenName
                    ]) {err in
                        if let err = err {
                            print ("Error writing document: \ (err)")
                        } else {
                            print ("Document successfully written!")
                            // Transition to HomeViewController via toMain
                            self.performSegue (withIdentifier: "toMain", sender: nil)
  • Answer # 1

    I feel strange how to create a Firestore instance.

    self.db.collection ("users")
    self.db.firestore (). collection ("users")

    Official documentation