如何在Swift中从另一个结构中编辑一个结构中的数组?

我在名为 WorkoutBuilderView 的结构中有一个“锻炼集”数组。我的问题是,我需要从嵌套在 WorkoutBuilderView 中的结构中访问该数组的内容(如下所示)。此结构不需要嵌套在 WorkoutBuilderView 中,但是它是首选。我唯一需要的是能够从另一个结构编辑 sets 数组。

可能为结构创建“ inout”类参数吗? (据我所知,仅在方法中有效)

  • 需要访问的数组称为 sets
  • 需要访问的位置由注释//访问点标记

代码:

struct WorkoutBuilderView: View {


@State var sets = [WorkoutSet(id: 0,percentage: -1,repetitions: -1,alteredValue: .max)]

var body: some View {
    Background {
        Group {
            ...
            if self.sets.count > 0 {
                ScrollView {
                    ForEach(self.sets) { set in
                        WorkoutSetLayout(index: set.id) //where sets needs to be passed in
                    }
                }

            }
            ...
        }
    }
    }

//this is the struct where the array needs to be accessed
struct BuilderPicker: View {

    let name: String
    let options: Array<String>
    let setIndex: Int


    @State var selectedOption = 0
    var body: some View {
        HStack {
            ...
        }

        .onReceive([self.selectedOption].publisher.first()) { (value) in

            //change sets here
            //access point
            print(value)

        }
    }

}

//layout (sets needs to pass through here too)
struct WorkoutSetLayout: View {
    let index: Int
    var body: some View {
        VStack(alignment: .leading) {
            Text("Set \(index + 1)")
            ...
            //the array needs to go into the BuilderPicker where it will be edited
            BuilderPicker(name: "Weight % of",options: [AlteredValue.max.rawValue,AlteredValue.weight.rawValue],setIndex: index)
            ...
        }
    }
}
//you probably don't need to worry about the Background
struct Background<Content: View>: View {
    private var content: Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content()
    }

    var body: some View {
        EmptyView()
            .frame(width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height)
            .overlay(content)
            .padding(.top,75)
    }
}


}
joel126 回答:如何在Swift中从另一个结构中编辑一个结构中的数组?

如果我正确理解了您的问题,@Binding正是用于此目的

1)将绑定var添加到嵌套结构,如

.
├── application.yml
├── js
│   ├── babel.config.js
│   ├── package.json
│   ├── package-lock.json
│   ├── postcss.config.js
│   ├── public
│   │   └── favicon.ico
│   ├── README.md
│   ├── src
│   │   ├── App.vue
│   │   ├── assets
│   │   │   └── logo.png
│   │   ├── components
│   │   │   └── HelloWorld.vue
│   │   ├── i18n.js
│   │   ├── locales
│   │   │   ├── en.json
│   │   │   └── kh.json
│   │   ├── main.js
│   │   ├── router
│   │   │   └── index.js
│   │   ├── store
│   │   │   └── index.js
│   │   └── views
│   │       ├── About.vue
│   │       └── Home.vue
│   └── vue.config.js
├── static
│   ├── assets
│   │   ├── css
│   │   │   └── about.e8edd4ed.css
│   │   ├── img
│   │   │   └── logo.82b9c7a5.png
│   │   └── js
│   │       └── about.b1f37908.js
│   └── dist
│       ├── bundle.js
│       ├── favicon.ico
│       ├── index.css
│       └── index.html
└── templates
    └── index.html

2)通过初始化程序将父级中的模型绑定为,

//layout (sets needs to pass through here too)
struct WorkoutSetLayout: View {
    let index: Int
    @Binding var data: [WorkoutSet]
    ...
本文链接:https://www.f2er.com/3169337.html

大家都在问