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 Improving Our User Interface Adding a Pop of Color

Christoph Müller
Christoph Müller
7,282 Points

App crashes after adding the random colors

Right after after adding the 'funFactButton' to my app, the app crashes when I click the button the first time.

The 'Debug Navigator' shows 'Thread 1: breakpoint 1.1' on the 'funFactLabel.text'-line in the 'showFunFact'-function.

@IBAction func showFunFact() { view.backgroundColor = colorWheel.randomColor() funFactButton.tintColor = colorWheel.randomColor() funFactLabel.text = factBook.randomFact() }

Do you have some advice how I should debug, since (as you can imagine) I double checked everything.

5 Answers

Christoph,

What does your view controller class look like? Here is what I have.

class ViewController: UIViewController {

  @IBOutlet weak var funFactLabel: UILabel!
  @IBOutlet weak var funFactButton: UIButton!

  let factBook = FactBook()
  let colorWheel = ColorWheel()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    funFactLabel.text = factBook.randomFact()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  @IBAction func showFunFact() {
    var randomColor = colorWheel.randomColor()
    view.backgroundColor = randomColor
    funFactButton.tintColor = randomColor
    funFactLabel.text = factBook.randomFact()
  }

}
Christoph Müller
Christoph Müller
7,282 Points

Aaron,

Thanks for your help. I can't say where the error was. But if I copy your code over mine it works. I checked line by line but was not able to found anything missing.

However, not it works.

Again, thanks for your help!

Christoph

Anthony Walker
Anthony Walker
4,448 Points

You are at the same position as me. The randomColor variable is not being accepted for some reason. The compiler wants randomColor() all the time. I haven't found a solution yet. What is the point if this is the easy app. What i do notice though is that the Treehouse folks are not that quick in helping despite everyone paying their subs

Anthony,

What does your code look like in ViewController.swift?

 @IBAction func showFunFact() {
        var randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        funFactLabel.text = factBook.randomFact()
    }
Anthony Walker
Anthony Walker
4,448 Points

// // ViewController.swift // Funfact // // Created by Tony Walker on 07/05/2015. // Copyright (c) 2015 Tony Walker. All rights reserved. //

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var funFactLabel: UILabel!
@IBOutlet weak var funFactButton: UIButton!

let factBook = FactBook()
let colorWheel = ColorWheel()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    funFactLabel.text = factBook.randomFact()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func showFunFact() {

    var randomColor = colorWheel.randomColor()
    view.backgroundColor = colorWheel.randomColor
    funFactButton.tintColor = colorWheel.randomColor
    funFactLabel.text = factBook.randomFact()
}

}

Doesn't work. Get two errors at

view.backgroundColor = colorWheel.randomColor funFactButton.tintColor = colorWheel.randomColor

Error I get is (function type return expected 'UIColor', did you mean to call it with '()'?)

@IBAction func showFunFact() {
        var randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        funFactLabel.text = factBook.randomFact()
    }

Replace your fun showFunFact() with mine above.

You have:

       var randomColor = colorWheel.randomColor()
        view.backgroundColor = colorWheel.randomColor
        funFactButton.tintColor = colorWheel.randomColor
        funFactLabel.text = factBook.randomFact()

It should be:

       var randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor // You don't need color wheel here 
        funFactButton.tintColor = randomColor // or here.
        funFactLabel.text = factBook.randomFact()
Anthony Walker
Anthony Walker
4,448 Points

Doh...............

What a dafty I am. Of course it does. Just missed him taking the whole lot out on the video. Well it beats missing a semi colon which is my usual fault in programming. Thanks Aaron. I am obviously hindered by my own lack of imagination.......

LOL it happens to all of us! Myself included. I don't miss the semi colons at all. I hatted them! Swift > Obj-c