UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 28 Project 4 part 3

Forums > 100 Days of SwiftUI

Hello fellow Swifters,

I am on the third part of project 4. In the last part of the project we are asked to

  1. Change the user interface so that it always shows their recommended bedtime using a nice and large font. You should be able to remove the “Calculate” button entirely.

In order to achieve this, I added a Text view with my preferences. I also made caluculateBedTime() to return a string now. I ran into an error:

Modifying state during view update, this will cause undefined behavior.

To metigate my issues, I made alertMessage local to my function.

I want to make sure if this is the right way to go about this. It does make sense to me that it is this way. This is the only place that I need this variable.

I also would like to make it that it only shows the calulcateBedTime when user has picked

  1. time when they want to wake up
  2. desired amount of sleep
  3. daily coffee intake

Thanks in advance :)

3      

You might receive two types of answers. Since this is a learning site, I'll try to provide you with a few hints that you can think about and try coding on your own.

Functions vs Computed Properties

Your calculateBedtime() function probably looks like this:

func calculateBedTime() {
    do {
      // === Wire up machine learning model =======================
      let config     = MLModelConfiguration()
      let model      = try SleepCalculator(configuration: config)
      // === Extract components from a date and time Calendar object =============
      let components = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)
      let hour       = (components.hour ?? 0) * 60 * 60
      let minute     = (components.minute ?? 0) * 60
      // === Perform a calculation ===========================
      let prediction     = try model.prediction(wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount)
      let sleepTime      = wakeUp - prediction.actualSleep
      recommendedBedTime = sleepTime
      // === Display a response =====================
      alertTitle   = "Your ideal bedtime is…"
      alertMessage = sleepTime.formatted(date: .omitted, time: .shortened)
  } catch {
      // === Display an error message ==================
      alertTitle   = "Error"
      alertMessage = "Sorry, there was a problem calculating your bedtime."
  } // end of do-catch
  showingAlert = true
  } // end of function

Because this is a function, you need a way to tell SwiftUI to execute it. Of course, the standard way might be to call this function by tapping a Button().

But what might you do to compute (<-- hint!) the bedtime value whenever any of the input variables change? You might want to review the lessons on computed variables!

// Function vs Computed Variable example
// Goal: Compute the area of a rectangle
var rectangleSideLength =  5.0
var rectangleBaseLength = 10.0

// You need a button to execute this function and calculate the area
func rectangleArea() -> Double {
    return rectangleSideLength * rectangleBaseLength // <-- Maths
}

// This declares a variable. But the value isn't stored...it's calculated.
// Whenever the length or the base change values, 
// the rectangleArea is automatically, and immediately calculated!
// If this variable is used in a view, the view redraws itself immediately.
var rectangleArea: Double {
    return rectangleSideLength * rectangleBaseLength  // <-- immediate calculation
}

Keep coding!

Hope this pushes you in the right direction!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.