gpt4 book ai didi

ios - 如何在 swiftUI 列表中更改复杂数据源

转载 作者:行者123 更新时间:2023-12-01 21:35:54 26 4
gpt4 key购买 nike

当我更改数据源时应用程序崩溃,例如点击 APIView 中的“更改数据”按钮或删除 QueryParametersView.list 中的项目
控制台日志:

This class 'SwiftUI.AccessibilityNode' is not a known serializableelement and returning it as an accessibility element may lead tocrashes

Fatal error: Index out of range: file/AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.8.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift,line 444

class URLComponentsModel: ObservableObject {
@Published var urlComponents = URLComponents.init()
var urlQueryItems: [URLQueryItem] {
get {
urlComponents.queryItems ?? [URLQueryItem].init()
}
set {
urlComponents.queryItems = newValue
}
}
}

struct APIView: View {
@ObservedObject var urlComponentsModel = URLComponentsModel.init()
var body: some View {
Button.init("change data") {
self.urlComponentsModel.urlComponents.queryItems?.removeFirst()
}
QueryParametersView.init(parameters: self.$urlComponentsModel.urlQueryItems)
}
}

struct QueryParametersView: View {
@Binding var parameters: [URLQueryItem]

var body: some View {
List {
ForEach(self.parameters.indices, id: \.self) { i in
HStack {
ParameterView.init(urlQueryItem: self.$parameters[i])
Text.init("delete")
.onTapGesture {
self.parameters.remove(at: i)
}
}
}
.onDelete { indices in
indices.forEach {
self.parameters.remove(at: $0)
}
}
}
}

struct ParameterView: View {
@Binding var urlQueryItem: URLQueryItem
var body: some View {
ZStack {
...
HStack {
...
if self.urlQueryItem.value != nil {
TextField("Value", text: Binding.init(get: {
(self.urlQueryItem.value ?? "")
}, set: { (value) in
self.urlQueryItem.value = value
}))
}
}
}
}
}
为什么?有人帮我吗?

最佳答案

removeFirst()
它说

The collection must not be empty.


如果调用 removeFirst 时集合为空,则应用程序会因索引超出范围而崩溃。

关于ios - 如何在 swiftUI 列表中更改复杂数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62737523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com