ios - SwiftUI - 错误 - 类型的值没有成员

标签 ios swift swiftui

我收到错误消息

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/

相关文章:

xcode - 显示 View 几秒钟(来自 NavController 的自定义推送)

SwiftUI - LaunchImage 未显示

javascript - Omniture/Adobe Analytics 跟踪破坏了 Safari 中的外部链接

ios - UIGraphicsGetImageFromCurrentImageContext() 不包括 CAAnimations

swift - 如何在 Swift 中使用 ABPeoplePickerNavigationController 选择联系人?

swiftui - 如何在 SwiftUI 中传递进度值(也许使用组合)?

animation - 全屏封面/模态的替代动画 - iOS 14

ios - 一个 View Controller 的状态栏样式

ios - 自定义事件指示器的基于计时器的动画的更好替代方案?

swift - 通过标签 ID 访问动态创建的 View