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 trialMichael Davenport
23,547 PointsBuild 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!
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
20,185 PointsHi 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! :)
Callum Lynch
5,972 PointsThe 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.
Ali Sutcu
Courses Plus Student 1,719 PointsHi 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
8,059 Pointsthose are the same Ali
Jeffrey Hallett
Courses Plus Student 1,619 PointsXavier actually one says 225.0 the other says 255.0 hard catch :)
Michael Davenport
23,547 PointsMichael Davenport
23,547 PointsThanks, Richard!
andrew naeve
11,503 Pointsandrew naeve
11,503 Pointswhen 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?