Silver Callenge: Dark Mode & "not enough excitement in ur life"

  // ConversionViewController.swift
 
   override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // CHALLENGE: Dark Mode
        let date = Date()
        let calendar = Calendar.current
        let hour = calendar.component(.hour, from: date)
        if hour > 17 || hour < 6 {
            self.view.backgroundColor = UIColor.darkGray
        } else {
            self.view.backgroundColor = UIColor.lightGray
        }
        // end... CHALLENGE: Dark Mode

        // CHALLENGE: "not enough excitement in your life."
         func getRandomColor() -> UIColor{
            //Generate between 0 to 1
            let red:CGFloat = CGFloat(drand48())
            let green:CGFloat = CGFloat(drand48())
            let blue:CGFloat = CGFloat(drand48())
            
            return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
        }
        // change randomly the color of the ConversionViewController's View
        self.view.backgroundColor = getRandomColor()
        // end... CHALLENGE: not enough excitement in your life.
} //end

Another approach

//    Silver Challenge: Dark Mode

// override func viewWillAppear(_ animated: Bool) {
// let hour = Calendar.current.component(.hour, from: Date())
// if hour > 6, hour < 19 {
// view.backgroundColor = UIColor.gray
// } else {
// view.backgroundColor = UIColor.darkGray
// }

// returns a random color
// func randomColor() -> UIColor {
// let red = CGFloat(drand48())
// let green = CGFloat(drand48())
// let blue = CGFloat(drand48())
// return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
// }
// view.backgroundColor = randomColor()
// }

Agreed.

Two interesting additions if there’s still not enough excitement in ur life :slight_smile: :

  1. To check if a integer is in a range one can also use the pattern-match operator in Swift and use a range:
    6 ... 17 ~= hour

  2. If you want the same light gray as used in the book, create a convenience initializer in Swift for UIColor and use UIColor(hex: “F5F4F1”) to create the same color as in the book. See https://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string for tips