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 Build a Simple iPhone App with Swift 2.0 Improving Our User Interface Random Colors

Daniel Yoo
Daniel Yoo
3,812 Points

Better Memory Allocation For Random Colors Just Like Fun Facts Model?

As we stored the property of factModel as an instance of FactModel() outside of the showFunFact() function to optimize memory usage as Pasan mentioned in the fact model video, should we not do the same for the color model? Like this:

@IBOutlet weak var funFactButton: UIButton!
let randomColor = ColorModel()

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

Is there an any advantage to write the code as Pasan wrote, which is:

@IBOutlet weak var funFactButton: UIButton!

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