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
jon kelson
5,149 PointsHi could someone explain just the last bit of code in this intermediate section please
Just this last bit is confusing me a little . basic terms please. many thanks
//this is the code from the last part up to the import UIKit
let titleLabel = UILabel()
func readingViewWithMode(readingMode: ReadingMode)
{
titleLabel.textColor = readingMode.headlineColor
}
import UIKit
enum ReadingMode {
case Day
case Evening
case Night
var statusBarStyle: UIStatusBarStyle {
switch self {
case .Day, .Evening: return .Default
case .Night: return .LightContent
}
}
var headlineColor: UIColor {
switch self {
case Night: return UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
case .Evening, .Day: return UIColor(red: 16/255.0, green: 16/255.0, blue: 16/255.0, alpha: 1.0)
}
}
var dateColor: UIColor {
switch self {
case .Day, .Evening: return UIColor(red: 132/255.0, green: 132/255.0, blue: 132/255.0, alpha: 1.0)
case .Night: return UIColor(red: 151/255.0, green: 151/255.0, blue: 151/255.0, alpha: 1.0)
}
}
var bodyTextColor: UIColor {
switch self {
case .Day, .Evening: return UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
case .Night: return UIColor(red: 151/255.0, green: 151/255.0, blue: 151/255.0, alpha: 1.0)
}
}
var linkColor: UIColor {
switch self {
case .Day, .Evening: return UIColor(red: 68/255.0, green: 133/255.0, blue: 164/255.0, alpha: 1.0)
case .Night: return UIColor(red: 129/255.0, green: 161/255.0, blue: 166/255.0, alpha: 1.0)
}
}
}
let titleLabel = UILabel()
func readingViewWithMode(readingMode: ReadingMode) {
titleLabel.textColor = readingMode.headlineColor
}
2 Answers
David Papandrew
8,386 PointsHi Jon,
If you're trying this code out in a Playground, be sure to call the function at the end of the code. This will help you understand what's going on (which I'll explain below).
You can call the function by typing in the following:
readingViewWithMode(.Night)
//you can change the "readingMode" parameter to any of the enum values: .Day, .Evening, .Night
So what's happening is this:
1) You are creating an instance of UILabel() called "titleLabel". We do this so that we can apply on of the ReadingMode text styles to the text color of this title label. In a real world example, the titleLabel might represent something like a new article headline or something.
2) The next line creates a function called "readingViewWithMode" that takes a parameter called readingMode of type ReadingMode. ReadingMode is a custom type. It is the enum you created in the first part of the code. So in order to use the function you will need to call the function and pass one of the enum values.
3) The next line contains the action the function performs.
titleLabel.textColor = readingMode.headlineColor
Here you are stating that you want the .textColor property of the titleLabel constant to be set to the .headlineColor property of the readingMode parameter being passed in to the function.
So if you call the function with: readingViewWithMode(.Night)
You can look at the enum code to see that the computed property for headlineColor has a switch statement that returns a specific color for the .Night case when the headlineColor property is used.
If you change the ReadingMode parameter to .Day, you will see that the playground outputs the color for the .Day case.
The enum also has three other properties, dateColor, bodyTextColor and linkColor. Try changing this line:
titleLabel.textColor = readingMode.headlineColor
to
titleLabel.textColor = readingMode.linkColor
and see what happens in the Playground.
Hope that helps.
jon kelson
5,149 Pointsfantastic answer thanks .I understand .correct me if I'm wrong, This is a big enum with lots of properties. I guess the UILabel() is just called from framework(i think its called ) as its not on the enum.
many thanks jon
David Papandrew
8,386 PointsCorrect, UILabel is a class from the UIKit framework.