swift - SwiftUI 从表单中删除项目导致崩溃
问题描述
我有一个数组Date()
,在我的数组中Form
我可以附加更多的日期。
@State private var dates: [Date] = [Date()]
但是当我尝试删除一个日期时,它会导致崩溃。Index out of range
. 当只剩下 1 个日期时,我禁用删除,所以这不是问题,因为此时您无法删除。
Section(header: Text("Notifications")) {
ForEach(dates, id: \.self) { date in
let value = dates.firstIndex(of: date)!
DatePicker("\(value + 1)", selection: $dates[value], displayedComponents: .hourAndMinute)
}.onDelete(perform: { (offsets) in
dates.remove(atOffsets: offsets)
})
.deleteDisabled(dates.count <= 1)
Button(action: {
dates.append(Date())
}, label: {
Text("Add Notification")
}).disabled(dates.count == 10)
}
为什么会崩溃?
解决方案
推荐阅读
- uitableview - 问题从具有多个部分的表中删除单行(Swift 4)
- c# - Windows 上的 MeasureString 与 .NET Core 中的 Ubuntu 不匹配
- amazon-web-services - 如何从 Amazon Connect 中的 amazon lex 响应访问 sessionAttributes 值?
- c - C:在 C 中执行和输出 shell 命令
- android - 如何以编程方式将 setStateListAnimator 用于我的复选框?
- javascript - 将 dc.js 显示到传单
- javascript - 使用 fetch 将跨域 JSON 数据发布到 Express 后端
- r - 用于最大化多维函数的高效优化函数或包
- linux - 识别进程的资源使用情况:CPU、内存和 I/O
- post - wordpress 注册帖子类型重写规则不起作用