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 trial

iOS Enhance a Weather App with Table Views Custom Table View Cells Adding Outlets to the Cell

Nicolas Moroz
Nicolas Moroz
76 Points

How to bring in variable from another function?

I am setting up my custom cell but would like to include a variable that I created in the retrieve weather function. For example, If I say "Let text = "test"" in the reviewWeather function, how can I include the variable "text" in the "cellForRowatIndexPath" function?

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

You need to declare the constant outside of the function if you want it to be accessed outside of the function. Probably somewhere at the top of your code. Then when you use them later, like inside the weather function or somewhere else, you don't declare them with the "let" keyword anymore, you just use the name of the constant by itself. So for example

let test = "test text"

func myFunction() {
    print(test)
}

func otherFunction() {
    let longerText = "this is some longer \(test)"
    print(longerText)
}