问题描述
我正在尝试为Apple Watch制作一个SwiftUI应用,该应用需要在ScrollView中使用倒数计时器。但是,当我在ScrollView中放置一个日期格式化程序时,该应用程序崩溃(至少在模拟器中-我无法在真实设备上进行测试,因为我已经达到了几天的应用程序ID限制)。
代码如下:
struct ContentView: View {
let date = Date()
var body: some View {
ScrollView {
Text(date,style: .timer)
}
}
}
它给了我这个运行时错误:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)
在名为SwiftUI.DisplayList.ViewUpdater.ViewCache.setNextUpdate(_:item:state:tag:)
的函数中
解决方法
这似乎是一个错误,似乎值得报告。
DateFormatter()
似乎是一种可行的解决方法。
import SwiftUI
struct TimerStyle: View {
let date = Date()
let timerFormat: DateFormatter
init() {
timerFormat = DateFormatter()
timerFormat.dateFormat = "HH:mm:ss"
}
var body: some View {
ScrollView {
Text(date,formatter: timerFormat)
}
}
}
struct TimerStyle_Previews: PreviewProvider {
static var previews: some View {
TimerStyle()
}
}
,
struct ContentView: View {
var body: some View {
ScrollView {
Text(Date(),style: .timer)
.clipped()
}
}
}
,
这就是答案!!!只需添加
.clipped()
struct ContentView: View {
var body: some View {
ScrollView {
Text(Date(),style: .timer)
.clipped()
}
}
}