100 Days of SwiftUI

Day 27

2026-05-15

Project 4, part 2

Building the app — “combining both SwiftUI and Core ML in remarkably few lines of code”

Building a basic layout

  • using a date picker and 2 steppers

I’m sure UI design will eventually come to me, but I don’t always get the look I expect in the simulator. I’m having to really trust the process here, because so much is handled by iOS that I don’t want to risk screwing something up down the road.

With PowerApps, it was pretty much “drag stuff around” and with HTML it was pretty much “you can’t do that”

Connecting SwiftUI to Core ML

  • “once you have a trained model you can get predictions in just two lines of code”…

Right, “trained” being the signifier here

Can you tell I am a bit of a skeptic?
  • you add the model BetterRest 1.mlmodel to the Project Navigator.

I thought it would be an Asset but I guess that’s primarily for pictures and stuff.

  • it automatically makes a Model Class of it

  • He suggests renaming the .mlmodel in the Project Navigator. I did, but then XCode couldn’t find it until I restarted it. TBH I bet I could have just renamed it in Finder. < goes off to check > … yup

When I put the mlmodel file directly into the project folder in Finder it also installed the class, but it wasn’t accessible in the same way (I didn’t see the file). Maybe it just hadn’t updated the view?

Well I tried it again and it wasn’t available in the project so I guess that was just a fluke.

Moral of the story: rename in Finder, drag to project in Project Navigator

  • add import CoreML to the top of the file before import SwiftUI. No operational reason to put it before SwiftUI, just to keep it alphabetical
  • the model uses wake (double), coffee (double), and estimated sleep (double) and outputs actual sleep (double) so we are going to have to do some conversions.

Heh. It was as easy as saying wake: Double(hour + minute), estimatedSleep: sleepAmount, coffee: Double(coffeeAmount) after calculating hour and minute

  • you can subtract a value in seconds directly from a Date, and you’ll get back a new Date

Cleaning up the user interface

  • set a default wakeTime instead of Date.now, making a static calculated parameter defaultWakeTime
  • static means the variable belongs to the ContentView and not just an instance of it

The code from the tutorial:

@State private var wakeUp = defaultWakeTime

What I did because I learned about AppStorage yesterday doing a MacOS SwiftUI tutorial 🤓

@AppStorage("wakeUp") var wakeUp = defaultWakeTime
  • to automagically have correct pluralization, use for example, "^[\(coffeeAmount) cup](inflect: true)"

It looks a lot like markdown footnote syntax but isn’t so don’t get confused!

 

WHOOPS I did the quiz and the challenge today, and it was supposed to be for Day 28.

Quiz

  • 12/12 😃

Challenge

  • Replace each VStack in our form with a Section, where the text view is the title of the section. Do you prefer this layout or the VStack layout? It’s your app – you choose!
  • Replace the “Number of cups” stepper with a Picker showing the same range of values.
  • 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.

My biggest issue is getting things laid out how I want to see them. I did have to do a lot of searching to figure out how to remove the “Calculate” button though.