Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStian Andreassen
523 PointsSwift3 - Get variable outside scope?
Hi,
I want to get the name and address from users in Firebase Database and write them back to Firebase in a different child.
I´m using this code to get the information I need:
`override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
let user = Auth.auth().currentUser!.uid
//Read data from Firebase
ref?.child("users").child(user).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDict = snapshot.value as? [String: Any],
let address = userDict["Address"] as? String,
let name = userDict["Name"] as? String else {
return
}
print(address + name)
})
}`
That is working fine, but I want to write the values back when a button is pushed. And I can’t figure out how to access the variables outside the scope? I´m sure this is a elementary question, but I will be very grateful for any help in the matter.
1 Answer
Balazs Peak
46,160 Pointstry separating declaration and adding value. Declare outside of the function, and add the value inside. If you are doing so, your scope will be "global" and you can reach the variable from anywhere.
Stian Andreassen
523 PointsStian Andreassen
523 PointsThank you for your answer. I have tried to declare outside, and adding inside, but it don´t work. I´m sure I´m doing it wrong. I have just startet the courses here in treehouse, but it hasn't covered this part yet. Could you please help me out some more?
Balazs Peak
46,160 PointsBalazs Peak
46,160 PointsOf course! Feel free to ask! you can have me on facebook, just in case if I do not reply. facebook.com/balazspukli
Stian Andreassen
523 PointsStian Andreassen
523 PointsCould you please help me out with declaring the constants outside the function so that they become globals, and I can reach them? I´m completely stuck with this :)
Balazs Peak
46,160 PointsBalazs Peak
46,160 PointsCould you please link the lesson/video you are refering to? I'm not familiar with swift very much, maybe I can work something out that way :)
Stian Andreassen
523 PointsStian Andreassen
523 PointsI´m sorry. This isn't part of any lessonI have done yet, this is a problem I´m struggling with on my own.
Balazs Peak
46,160 PointsBalazs Peak
46,160 PointsWell, I'm thinking of something like this... I hope this makes sense and you can realize what did I different. Declaration in the outer scope, adding value inside...
`override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
let user = Auth.auth().currentUser!.uid
let address
let name
//Read data from Firebase
ref?.child("users").child(user).observeSingleEvent(of: .value, with: { (snapshot) in
}`