尝试在swift中将字典数组从视图A传递到第二个视图B。
在视图A中,使用视图中的隐藏导航链接推送视图B,并将arrayMergeT100发送到视图B
@State var arrayMergeT100 = [[String:Any]]()
NavigationLink(destination:ShowBookChoices(bookOptions: self.$arrayMergeT100),
isActive: self.$pushActive) {
EmptyView()
}.hidden()
视图B,尝试访问键“概要”的值,并在此行上收到两个错误“if let index=bookOptions.index(forKey:“概要”){”。错误1-调用中的参数标签不正确(有'forKey:',预期为'where:'),错误2-无法将'String'类型的值转换为预期的参数类型'([String:Any]),抛出->Bool'。将forKey:更改为where:不会使Error2消失
struct ShowBlindDateChoices: View {
@Binding public var bookOptions: [[String:Any]]
var body: some View {
VStack{
if let index = bookOptions.index(forKey: "synopsis") {
let _ = print(bookOptions[index].key, ":", bookOptions[index].value)
}