我正在使用SwiftUI的拖放修饰符。因为,iOS 15提供了自定义onDrag修饰符的选项,我正在使用它,如下所示。
问题:,我有一个if #available(iOS 15.0, *)保护程序,可以为不同的iOS版本选择视图。但是,我在iOS 14.x上的应用程序由于EXC_BAD_ACCESS (code=1, address=0x0)错误而崩溃,因为它试图找到新的API,但它不在iOS 14中。
@ViewBuilder
private func getRoutineCard(routine: RoutineInfo) -> some View {
if #available(iOS 15.0, *) {
newRoutineCard(routine: routine)
} else {
oldRoutineCard(routine: routine)
}
}
@available(iOS 15.0, *)
@ViewBuilder
private func newRoutineCard(routine: RoutineInfo) -> some View {
RoutineCard(routine: routine)
// This modifier is what crashes the app on iOS 14.x
// onDrag with a drag preview view not available in iOS 14.x
.onDrag({
dragReorder(draggedRoutine: routine)
}, preview: {
RoutineCardDragPreview(routine: routine)
})
.onDrop(of: [.text], delegate: ReorderDropDelegate(isDragging: $isDragging, draggedItem: $draggedItem, item: routine, haptics: haptics, onMove: updateOrder(routine:order:)))
}
@ViewBuilder
private func oldRoutineCard(routine: RoutineInfo) -> some View {
RoutineCard(routine: routine)
.onDrag { dragReorder(draggedRoutine: routine) }
.onDrop(of: [.text], delegate: ReorderDropDelegate(isDragging: $isDragging, draggedItem: $draggedItem, item: routine, haptics: haptics, onMove: updateOrder(routine:order:)))
}我正在寻找的内容:是一个解决方案,可以根据iOS版本在两个视图之间进行选择,而不会导致崩溃。
发布于 2022-01-06 07:21:47
任何无意中发现这一点的人:
当在iOS中使用if #available( ViewBuilder 14.0,OSX11.0,*)时,应用程序在后台运行时会崩溃。
在Xcode版本13.1及以上版本中会出现这种情况。(目前的Xcode 13.2.1)用Xcode 13.0编译和崩溃进行了测试。我们认为这是因为Xcode中的编译器优化错误。对于开发构建,我们没有这些崩溃。
更多信息:https://swiftui-lab.com/bug-os-check/和相同的答案:Swiftui app crashes with different SearchBar (ViewModifier) on iOS 14 / 15
发布于 2022-04-18 06:50:30
这要归功于用ViewBuilder编写的代码。需要转移到视图修饰符
https://stackoverflow.com/questions/70499011
复制相似问题