Setting UILabels to Firestore data

Hi,
I would like to be able to take two fields of a firestorm document and then set two UILabels to those fields. Here is my code:

Code Block import UIKit
import SafariServices
import WebKit
import Firebase
class TheRiddle: UIViewController {
    
    @IBOutlet weak var leading_TheRiddle: NSLayoutConstraint!
    @IBOutlet weak var trailing_TheRiddle: NSLayoutConstraint!
    
    @IBOutlet weak var CurrentRiddle: UILabel!
    @IBOutlet weak var PreviousRiddle: UILabel!
    
    var menuOut = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let db = Firestore.firestore()
        
        db.collection("TheRiddle").document("Riddles").getDocument { (document, error) in
            
            //check for error
            if error == nil {
                //check if document exists
                if document != nil {
                }else {
                    if let currentRiddle = document!.get("CurrentRiddle") as? String {
                        self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle
                    }
                    if let previousRiddle = document!.get("CurrentRiddle") as? String {
                        self.CurrentRiddle.text = "Previous Riddle: " + previousRiddle
                    }
                }
            }
            
        }
        
        
    }
    
    
    @IBAction func RiddleAnswerForm(_ sender: Any) {
        let vc = SFSafariViewController(url: URL(string: "https://forms.office.com/Pages/ResponsePage.aspx?id=ytAqTDte6UK-KD5v_kOm4Y843IzqmYtFlDtLrfRYsi1UMFpUMk1GN01GS05BVFlJUElONk4yR1hKUCQlQCN0PWcu")!)
        
        present(vc, animated: true)
    }
    
    @IBAction func menuTappedTheRiddle(_ sender: Any) {
        
        if menuOut == false {
            leading_TheRiddle.constant = 150
            trailing_TheRiddle.constant = -150
            menuOut = true
        }
        else {
            leading_TheRiddle.constant = 0
            trailing_TheRiddle.constant = 0
            menuOut = false
        }
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
            self.view.layoutIfNeeded()
        }) { (animationComplete) in
            print("The animation is complete")
        }
}
}

Thanks in advance!
Answered by OOPer in 661250022

”!=“ is not, right? Thats what I have learned. 

Didn't you read my replies?

Your code (updated one):
Code Block
if snapshot != nil {
//# Doing nothing when `snapshot` is *not nil*
}else {
//# This code block is executed when `snapshot` *is nil*
//...
}

is equivalent to:
Code Block
if snapshot == nil {
//# This code block is executed when `snapshot` *is nil*
//...
}


You may need to learn how if-else works,
I don't think my app is connected to Firestore, how would I double check this?

I don't think my app is connected to Firestore, how would I double check this?

Firestore is not a framework of Apple's. You should better visit the supporting site or the community site of it to get better responses sooner.
Update, it is connected but I am not using the correct code. What code would i use to get the data of a specific data?

What code would i use to get the data of a specific data?

Is the code you are using now exactly the same as the code in your opening post?

This is the most up to date code

Code Block import UIKit
import SafariServices
import WebKit
import FirebaseFirestore
import Firebase
class TheRiddle: UIViewController {
    
    @IBOutlet weak var leading_TheRiddle: NSLayoutConstraint!
    @IBOutlet weak var trailing_TheRiddle: NSLayoutConstraint!
    
    @IBOutlet weak var CurrentRiddle: UILabel!
    @IBOutlet weak var PreviousRiddle: UILabel!
    
    var menuOut = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let db = Firestore.firestore()
        
        print("TheRiddle collection: ", db.collection("TheRiddle").document("Riddles"))
        
        db.collection("TheRiddle").document("Riddles").addSnapshotListener { snapshot, error in
                
            //check for error
            if error == nil {
                //check if document exists
                if snapshot != nil {
                }else {
                    if let currentRiddle = snapshot?["CurrentRiddle"] as? String {
                        self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle
                        print(currentRiddle)
                    }
                    if let previousRiddle = snapshot?["CurrentRiddle"] as? String {
                        self.PreviousRiddle.text = "Previous Riddle: " + previousRiddle
                    }
                }
            }
            
        }
        //db.collection("test").addDocument(data: ["hi":"bye"])
    }
    
    
    @IBAction func RiddleAnswerForm(_ sender: Any) {
        let vc = SFSafariViewController(url: URL(string: "https://forms.office.com/Pages/ResponsePage.aspx?id=ytAqTDte6UK-KD5v_kOm4Y843IzqmYtFlDtLrfRYsi1UMFpUMk1GN01GS05BVFlJUElONk4yR1hKUCQlQCN0PWcu")!)
        
        present(vc, animated: true)
    }
    
    @IBAction func menuTappedTheRiddle(_ sender: Any) {
        
        if menuOut == false {
            leading_TheRiddle.constant = 150
            trailing_TheRiddle.constant = -150
            menuOut = true
        }
        else {
            leading_TheRiddle.constant = 0
            trailing_TheRiddle.constant = 0
            menuOut = false
        }
        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations: {
            self.view.layoutIfNeeded()
        }) { (animationComplete) in
            print("The animation is complete")
        }
}
}


This is the most up to date code

Thanks for updating the code.

I have no knowledge and no intention to go deep into Firestore, but the lines 56...74 of your code is equivalent to the following:
Code Block
if snapshot == nil {
if let currentRiddle = snapshot?["CurrentRiddle"] as? String {
self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle
print(currentRiddle)
}
if let previousRiddle = snapshot?["CurrentRiddle"] as? String {
self.PreviousRiddle.text = "Previous Riddle: " + previousRiddle
}
}

The two if-lets are executed only when snapshot is nil, so, it has no meaning.

You might want to do something like this:
Code Block
if let snapshot = snapshot {
if let currentRiddle = snapshot["CurrentRiddle"] as? String {
self.CurrentRiddle.text = "This Weeks Riddle: " + currentRiddle
print(currentRiddle)
}
if let previousRiddle = snapshot["CurrentRiddle"] as? String {
self.PreviousRiddle.text = "Previous Riddle: " + previousRiddle
}
}


Thanks, but as you can see it says not nil, other ideas?

as you can see it says not nil

Sorry, but I cannot see anything that is saying not nil.
”!=“ is not, right? Thats what I have learned.
Accepted Answer

”!=“ is not, right? Thats what I have learned. 

Didn't you read my replies?

Your code (updated one):
Code Block
if snapshot != nil {
//# Doing nothing when `snapshot` is *not nil*
}else {
//# This code block is executed when `snapshot` *is nil*
//...
}

is equivalent to:
Code Block
if snapshot == nil {
//# This code block is executed when `snapshot` *is nil*
//...
}


You may need to learn how if-else works,
Thank you for your patience with me and sorry for the misunderstanding, I am new to swift and just didn't see it. However it is working now and I am very grateful!

However it is working now and I am very grateful!

Happy to hear that. Frankly I was at a loss how I should tell what is wrong...
I will try better if I have another chance. Good luck.
Setting UILabels to Firestore data
 
 
Q