我收到错误消息
Value of type '[Voucher]' has no member 'currency'.
Value of type '[Voucher]' has no member 'shopName'.
Value of type '[Voucher]' has no member 'value'.
这里我收到错误(ContentView):
@ObservedObject var voucherData = VoucherData()
var body: some View {
NavigationView {
ZStack {
List(voucherData.voucherList) { voucher in
NavigationLink(destination: EditView(value: self.$voucherData.voucherList.value, currency: self.$voucherData.voucherList.currency, shopName: self.$voucherData.voucherList.shopName)) {
VStack() { ...
在另一个文件中定义了列表:
import SwiftUI
import Combine
struct Voucher : Identifiable {
var value : String = ""
var currency : String = ""
var shopName : String = ""
}
final class VoucherData: ObservableObject {
@Published var voucherList: [Voucher] = [
.init(value: "20", currency: "USD", shopName: "FlyBurger")]
}
最佳答案
这是因为您试图访问数组而不是单个对象,请将代码更改为
var body: some View {
NavigationView {
ZStack {
List(voucherData.voucherList) { voucher in
NavigationLink(destination: EditView(value: voucher.value, currency: voucher.currency, shopName: voucher.shopName)) {
VStack() { ...
也就是说,由于 List 会迭代您提供的数组 voucherData.voucherList
,并将每个元素作为参数发送给闭包 voucher
,因此您应该使用闭包内的参数
关于ios - SwiftUI - 错误 - 类型的值没有成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598351/