如何在数组中切换对象的属性并更新SwiftUI视图?

时间:2020-05-16 20:33:04

标签: arrays swift xcode view swiftui

SwiftUI问题。我有这段代码,给我错误:“编译器无法在合理的时间内对这种表达式进行类型检查;尝试将表达式分成不同的子表达式”。该错误是由于feel.selected.toggle()引起的。 myFeelings是一个ObservableObject类,其中包含作为结构的Feeling数组。我希望能够在按下按钮并更新视图时将selected属性更改为true或false。我不知道是否有人可以帮助我,我通常是编码方面的新手。

ForEach(myFeelings.feelings.indices) { index in
                    ZStack {
                        Button(action: {
                            self.myFeelings.feelings[index].selected.toggle()
                            print("A")
                        }) {
                            Text(self.myFeelings.feelings[index].emoji)
                                .font(.system(size: 40))
                        }
                        Text(String(self.myFeelings.feelings[index].selected))
                        .font(.system(size: 10))
                    }
                }

这是我要根据所选感受更新的部分。

ForEach(myFeelings.feelings.indices) { index in
                        Group {
                            if self.myFeelings.feelings[index].selected {
                            ZStack {

                                        Rectangle()
                                        .frame(height: 100)
                                            .foregroundColor(self.myFeelings.feelings[index].color)
                                        .scaledToFill()
                                    }

                            }
                        }

                    }

这是数组:

    @State var myFeelings = Feelings(feelings:[Feeling(feeling: "Joy", emoji: "?", color:  Color(#colorLiteral(red: 0.9607843137254902, green: 0.7058823529411765, blue: 0.2, alpha: 1.0))),
        Feeling(feeling: "Trust", emoji: "?", color: Color(#colorLiteral(red: 0.4666666666666667, green: 0.7647058823529411, blue: 0.26666666666666666, alpha: 1.0))),
        Feeling(feeling: "Fear", emoji: "?", color: Color(#colorLiteral(red: 0.27450980392156865, green: 0.48627450980392156, blue: 0.1411764705882353, alpha: 1.0))),
        Feeling(feeling: "Surprise", emoji: "?", color: Color(#colorLiteral(red: 0.12941176470588237, green: 0.21568627450980393, blue: 0.06666666666666667, alpha: 1.0))),
        Feeling(feeling: "Sadness", emoji: "?", color: Color(#colorLiteral(red: 0.17647058823529413, green: 0.4980392156862745, blue: 0.7568627450980392, alpha: 1.0))),
        Feeling(feeling: "Disgust", emoji: "?", color: Color(#colorLiteral(red: 0.12156862745098039, green: 0.011764705882352941, blue: 0.4235294117647059, alpha: 1.0))),
        Feeling(feeling: "Anger", emoji: "?", color: Color(#colorLiteral(red: 0.7450980392156863, green: 0.1568627450980392, blue: 0.07450980392156863, alpha: 1.0))),
        Feeling(feeling: "Anticipation", emoji: "?", color: Color(#colorLiteral(red: 0.9372549019607843, green: 0.34901960784313724, blue: 0.19215686274509805, alpha: 1.0)))])

感觉和感觉是结构

1 个答案:

答案 0 :(得分:1)

在您的foreach中使用https://pypi.org/project/django-bootstrap-datepicker-plus/,并使用@State声明myFeelings,这是一个完整的示例:

struct FeelingGroup {
    var feelings: [Feeling]
}

struct Feeling {
    var selected: Bool
    var emoji: String
}
struct ToggleView: View {
    @State var myFeelings = FeelingGroup(feelings: [Feeling(selected: true, emoji: "A"),
                                                    Feeling(selected: true, emoji: "B"),
                                                    Feeling(selected: false, emoji: "C")])
    var body: some View {
        VStack{
            ForEach(myFeelings.feelings.indices) { index in
                HStack {
                    Button(action: {
                        self.myFeelings.feelings[index].selected.toggle()
                        print("A")
                    }) {
                        Text(self.myFeelings.feelings[index].emoji)
                            .font(.system(size: 40))
                    }
                    Text(String(self.myFeelings.feelings[index].selected))
                    .font(.system(size: 40))
                }
            }
        }
    }
}

struct ToggleView_Previews: PreviewProvider {
    static var previews: some View {
        ToggleView()
    }
}