简体   繁体   中英

How can I toggle a property of an object in an array and update the SwiftUI view?

SwiftUI Question. I have this code that gives me the error “The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions”. The error is because of the feeling.selected.toggle(). myFeelings is a ObservableObject Class that contains an array of Feeling which is a struct. I want to be able to change the selected property to true or false when pressing the button and update the view. I don't know if somebody can help me, I am new with coding in general.

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))
                    }
                }

This is the part I want to update with the selected feelings.

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()
                                    }

                            }
                        }

                    }

Here is the array:

    @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)))])

Feeling and Feelings are Structures

Use indices in your foreach, and declare myFeelings with @State, here is a full working example:

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM