I've just bought a M2 MacBook Pro, and my Timer app is giving me strange crash logs.
Here's my code:
struct ContentView: View {
@State var process: Any? = nil
@AppStorage("time") var time = 0
@State var timePassed = 0
@State var timer: Timer.TimerPublisher = Timer.publish(every: 1, on: .main, in: .common)
@State var cacheSecondsString: String = String(UserDefaults.standard.integer(forKey: "time"))
@State var startTheTimer: Bool = false
var body: some View {
ZStack {
TimerView(time: $timePassed, total: $time).padding()
VStack {
TextField("Time...", text: $cacheSecondsString)
.font(.system(size: 35, weight: .light, design: .rounded))
.multilineTextAlignment(.center)
.onSubmit {
time = Int(cacheSecondsString) ?? time
cacheSecondsString = String(time)
}
.textFieldStyle(.plain)
.padding()
.fixedSize()
Button {
withAnimation {
startTheTimer.toggle()
}
} label: {
Label(startTheTimer ? "Stop" : "Start", systemImage: startTheTimer "stop.fill" : "clock").foregroundColor(.accentColor)
}.buttonStyle(.plain).padding()
}
}.onChange(of: startTheTimer) { start in
if start {
process = timer.connect()
} else {
(process! as! Cancellable).cancel()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice("iPad Air (5th generation)")
}
}
When I sort of "run" the preview (pressing on the play.circle
button on top of my preview and press the button the second to deactivate my timer, a crash dialog appear, the standard one macOS uses when any process crashed, but the preview didn't crash.