这篇文章介绍了作者在学习 SwiftUI 时的第四个项目,主要涉及时间选择器的样式、读取和写入时间的特定部分以及使用 do catch 结构抓取错误等内容。同时,作者也分享了第二遍练习的代码。
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
投诉这个时间这部分真的太难了。再练习一遍。增强熟练度。
时间选择器样式
时间选择器隐藏文本标签,时间选择器只显示小时和分钟。
1 2 3
| DatePicker(selection: $wakeUp, displayedComponents: .hourAndMinute, label: { Text("Please enter a date") }) .labelsHidden() .datePickerStyle(WheelDatePickerStyle())
|
读取和写入时间的特定部分
1 2 3 4
| var components = DateComponents() components.hour = 8 components.minute = 0 let date = Calendar.current.date(from: components)
|
有可能出错的情况下使用 do catch 结构抓取错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| do { let sleepTime = try model.prediction(wake: time, estimatedSleep: Double(self.sleepTimeLength), coffee: Double(self.coffeeCups)) let bestSleepTime = self.wakeDate - sleepTime.actualSleep let formatter = DateFormatter() formatter.timeStyle = .short let dateString = formatter.string(from: bestSleepTime) self.alertTitle = "你的睡眠时间应该在" self.alertMessage = "\(dateString)" self.alertShow.toggle() }catch{ self.alertTitle = "出现了未知错误" self.alertMessage = "无法计算出推荐睡眠时间" self.alertShow.toggle()
|
第二遍练习代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
import SwiftUI
struct ContentView: View { @State private var wakeDate = compDate @State private var sleepTimeLength: Double = 8 @State private var coffeeCups = 1 @State private var alertShow = false @State private var alertTitle = "" @State private var alertMessage = "" static var compDate: Date { var com = DateComponents() com.hour = 8 com.minute = 0 return Calendar.current.date(from: com) ?? Date() } var body: some View { NavigationView { Form { VStack(alignment: .leading) { Text("你什么时候想起床?") .bold() DatePicker(selection: $wakeDate,displayedComponents: .hourAndMinute, label: { Text("你什么时候想起床?") }) .labelsHidden() .datePickerStyle(WheelDatePickerStyle()) } VStack(alignment: .leading) { Text("期望睡眠时长") .bold() Stepper(value: $sleepTimeLength, in: 4...12, step: 0.25) { Text("\(self.sleepTimeLength, specifier: "%.2f") 小时") } } VStack(alignment: .leading) { Text("每日饮用咖啡杯数") .bold() Stepper(value: $coffeeCups, in: 1...20) { Text("\(self.coffeeCups) 杯") } } } .navigationBarTitle(Text("最佳睡眠时间计算")) .navigationBarItems(trailing: Button(action: { let model = SleepCalculator() let wakeTime = Calendar.current.dateComponents([.hour,.minute], from: self.wakeDate) let hour = (wakeTime.hour ?? 0) * 60 * 60 let minute = (wakeTime.minute ?? 0) * 60 let time = Double(hour + minute) do { let sleepTime = try model.prediction(wake: time, estimatedSleep: Double(self.sleepTimeLength), coffee: Double(self.coffeeCups)) let bestSleepTime = self.wakeDate - sleepTime.actualSleep let formatter = DateFormatter() formatter.timeStyle = .short let dateString = formatter.string(from: bestSleepTime) self.alertTitle = "你的睡眠时间应该在" self.alertMessage = "\(dateString)" self.alertShow.toggle() }catch{ self.alertTitle = "出现了未知错误" self.alertMessage = "无法计算出推荐睡眠时间" self.alertShow.toggle() } }) { Text("计算") }) } .alert(isPresented: $alertShow){ Alert(--- title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("确定"))) } } }
struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
|
SwiftPlayground 进度