Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

I am not sure why I am not able to do this on my code

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var funFactLabel: UILabel!

@IBOutlet weak var funFactButton: UIButton!

let factModel = FactModel()


override func viewDidLoad() {
    super.viewDidLoad()
    funFactLabel.text = factModel.getRandomFact()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

@IBAction func showFunFact() {
    let randomColor = ColorModel().getRandomColor()
    view.backgroundColor = randomColor
    funFactButton.tintColor = randomColor
    funFactLabel.text = factModel.getRandomFact()
}

}

Hey guys, I have no problem with the code I am just trying to figure out why something won't work if I change it. Why can I not move the let randomColor = ColorModel().get RandomColor() to the top where FactModel is. and then just reference it when needed like I did with factModel. For some reason when I do that I get the random colors but they do not change after.

2 Answers

When the declaration of randomColor is in the function, your code will call the getRandomColor() function each time the showFunFact() method is called.

@IBAction func showFunFact() {
    let randomColor = ColorModel().getRandomColor()
    view.backgroundColor = randomColor
    funFactButton.tintColor = randomColor
    funFactLabel.text = factModel.getRandomFact()
}

If you put the declaration in the ViewController class, rather than the function, how many times will getRandomColor() be called?

Addison Francisco
Addison Francisco
9,561 Points

First, let's look at what the showFunFact() method is doing. It's purpose is to display a new fact, update the button text color, and update the background color. So, when we call the showFunFact() method, it first creates an instance of ColorModel, then calls it's getRandomColor() method which returns a UIColor from your ColorModel.colors array. This is the value that is set to randomColor

let randomColor = ColorModel().getRandomColor()

Next, we set view.backgroundColor using the value we assigned to randomColor

view.backgroundColor = randomColor

We also want the funFact text color to be the same as the background color, so we use the value stored in randomColor again to set this

funFactButton.tintColor = randomColor

With that in mind, let's go back to the original question of why we can't place let randomColor = ColorModel().getRandomColor() outside of the showFunFact() method. If we set the value returned from getRandomColor() outside of the function body, on the line below let factModel = FactModel(), we are only setting that value once. So, if we reference randomColor within showFunFact, the value will always be the same because we only called the getRandomColor() method one time. When we place let randomColor = ColorModel().getRandomColor() within the showFunFact() function, we are getting a new color every time showFunFact() is called.

I hope this is helpful for you.