100 Days of SwiftUI
2026-05-14
Project 4, part 1
BetterRest, a Machine Learning project. Since the tutorial was written in 2023, it seems somehow quaint.
But we will be working with Core ML (regression analysis) so that will be interesting.
Techniques
Stepper
Stepperuses+-buttons- can be bound to
IntandDouble - can be limited to a range
- steps size can be set (as long as it is the same type number as the stepper definition)
- add
.formatted()to the value text to get the numbers formatted better for display
DatePicker
DatePickerreturns a tappable control for dates and times right out of the boxDate.nowreturns the current date and timelabelsHidden()can be used as a modifier to hide a label but make it available to a screen reader- add
displayedComponentsto the call to define what should be shown e.g.DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)will allow the user to select a precise time - can use dates as ranges, including one-sided ranges e.g.
DatePicker("Please enter a date", selection: $wakeUp, in: Date.now...)will let the user select any date/time from now into the future, but not the past.
Working with dates
- DATES ARE HARD
- Lean on Apple’s frameworks for date calculations and formatting
Datereturns an entire date including year, month, date, hour,….DateComponentsgives us just the bit we care aboutlet date = Calendar.current.date(from: components) ?? .now(coalescing becausedate(from:)returns an optional date)- we can ask for the hour and minute, but we’ll be handed back a
DateComponentsinstance with optional values for all of its properties. So we might do something like:
let components = Calendar.current.dateComponents([.hour, .minute], from: someDate)
let hour = components.hour ?? 0
let minute = components.minute ?? 0- the User’s region settings will determine how formatted dates are presented, we just need to request the proper type (e.g.
Text(Date.now, format: .dateTime.day().month().year())will show “May 14, 2026” on my system even thoughdayis called first.) - there are other formatting options to play around with (in the form of
Text(Date.now.formatted(date: .numeric, time: .shortened)))
Create ML and Core ML
- there are 2 Apple frameworks for ML — Core ML and Create ML
- Create ML is for training models; Core ML runs the models in your app
- Create ML is its own app
- Watch an old video on Create ML that describes the various ML methods offered (Random Forest, Boosted Tree, Decision Tree, and Linear Regression)
- It creates its own training/validation data split unless you specify differently
- It optimizes for root mean squared error