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 Changing the Background Color

Michael Davenport
Michael Davenport
23,547 Points

Build a Simple iPhone App in Swift 2.0 - Changing the Background Color

I'm having trouble with Part 2 of this Code Challenge. I thought I could change the background color of the view by accessing this property using dot notation (i.e., view.backgroundColor); however, I'm receiving an error: expected declaration.

Any help is greatly appreciated!

colors.swift
class ViewController: UIViewController {

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    let blueColor = UIColor.blueColor()
    view.backgroundColor = blueColor
}

3 Answers

Richard Lu
Richard Lu
20,185 Points

Hi Michael,

The reason you're getting an error in this challenge is because you're placing the snippet of code in a class, but that class doesn't know what to do with it. In order to have your code execute, you can try placing it in a method. Like this:

class ViewController: UIViewController {

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        let blueColor = UIColor.blueColor()
        view.backgroundColor = blueColor
    }

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

Happy coding, and good luck! :)

when i try to run this code it fails:

let blueColor = UIColor(red: 0/225.0, green: 0/225.0, blue: 255/225.0, alpha: 1.0)

view.backgroundColor = blueColor

but

let blueColor = UIColor.blueColor()

view.backgroundColor = blueColor

runs fine. Am I messing something up here?

The answer given above in not correct due to the constant needing to be outside of the function. I have attached the correct code below via a link. I hope this helps.

http://imgur.com/a/0ip3k

Hi Andrew if you change "UIColor(red: 0/225.0, green: 0/225.0, blue: 255/225.0, alpha: 1.0)" with "UIColor(red: 0/255.0, green: 0/255.0, blue: 255/255.0, alpha: 1.0)" it should work.

Xavier Avery
Xavier Avery
8,059 Points

those are the same Ali

Xavier actually one says 225.0 the other says 255.0 hard catch :)