1.9 C
New York
Friday, January 31, 2025

swift – Foreach loop inside button motion block throws “Kind() can’t conform to view”


I’ve an array of views that I must get values from in an effort to save them to my context. When making an attempt to loop over these when a button is clicked I get a “Kind ‘()’ can’t conform to ‘View’; solely struct/enum/class sorts can conform to protocols” exception. Im very new to swift and I’m questioning if there’s a better method to do that. Thanks in superior!

That is the entire view:

struct DetailedView: View {
    
    @Atmosphere(.managedObjectContext) var moc
    
    var targetMuscle : String = "Chest"
    let at the moment = Date()
    @State public var exerciseCards : [ExerciseCard] = []
    @State public var train : String = "Bench Press"
    @State public var workout routines : Int = 0
    @State public var showPassedWorkouts : Bool = false
    
    static let taskDateFormat : DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .lengthy
        return formatter
    }()
    
    var physique: some View {
        ZStack{
            VStack{
                HStack{
                    VStack{
                        Textual content(targetMuscle).font(.system(dimension:40)).fontWeight(.medium)
                        Textual content("(at the moment, formatter: Self.taskDateFormat)")
                            .font(.system(dimension:20))
                    }.body(width: 250, top: 30, alignment: .topLeading)
                        .navigationBarTitle("")
                        .navigationBarHidden(true)
                        .padding(.backside, -7)
                    Button(motion: {
                        self.showPassedWorkouts.toggle()
                    }) {
                        Textual content("Handed Exercises")
                            .multilineTextAlignment(.middle)
                        
                        
                    }.offset(x: -75, y: 25)
                        .sheet(isPresented: $showPassedWorkouts){
                            PassedWorkoutList()
                    }
                    
                    
                    Button(motion: {
                        let exercise = Exercise(context: self.moc)
                        exercise.muscle = self.targetMuscle
                        exercise.date = formattedDateString(day: self.at the moment)
                        
                        ForEach(0.. String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = format
    return dateFormatter.string(from: day)
}

The above view additionally references two views the primary one being the ExerciseCard view

struct ExerciseCard: View {
    @State public var train : String = "Bench Press"
    @State public var tableRows : [ExerciseTableRow] = []
    
    var physique: some View {
        VStack{
            TextField("Enter Train", textual content: $train).textFieldStyle(RoundedBorderTextFieldStyle())
            .body(width: 300)
            .multilineTextAlignment(.middle)
            HStack{
                Group{
                    Textual content("Set")
                    Textual content("Weight")
                    Textual content("Reps")
                }.padding(.horizontal, 30)
                .offset(x: -20, y: 0)
                
            }
            VStack{
                
                ForEach(0.. 1{
                        self.tableRows.take away(at: self.tableRows.count-1)
                    }
                        
                }) {
                    Textual content("Take away Set")
                        .body(minWidth: 150)
                        .padding(.vertical, 5)
                        .foregroundColor(.white)
                        .background(Colour.pink)
                        .cornerRadius(20)
                    
                }
                
                Button(motion: {
                    self.tableRows.append(ExerciseTableRow(set: 2, readOnly: false, setWeight: 2, setReps: 2))
                }) {
                    Textual content("Add Set")
                        .body(minWidth: 150)
                        .padding(.vertical, 5)
                        .foregroundColor(.white)
                        .background(Colour.inexperienced)
                        .cornerRadius(20)
                    
                }
                
            }
        }
        .padding()
        .padding(.vertical)
        .background(Colour.offWhite)
        .cornerRadius(20)
        .shadow(shade: Colour.black.opacity(0.2), radius: 10, x:10, y:10)
        .shadow(shade: Colour.white.opacity(0.7), radius: 10, x:-5, y:-5)
    }
}

The second being the ExerciseTableRow view

struct ExerciseTableRow: View {
    @State public var weight : String = "0"
    @State public var reps : String = "0"
    var set : Int16
    var readOnly : Bool
    var setWeight : Int16
    var setReps : Int16
    var physique: some View {
        
        HStack{
            Textual content(String(set))
                .padding(.trailing, 40)
                .padding(.main, 10)
            if readOnly == false{
                Group{
                    TextField("0", textual content: $weight)
                    TextField("0", textual content: $reps)
                }.textFieldStyle(RoundedBorderTextFieldStyle())
                    .body(width: 50)
                    .multilineTextAlignment(.middle)
                    .keyboardType(.numberPad)
                    .padding(.horizontal, 30)
            }
            else{
                Group{
                    Textual content(String(setWeight))
                    Textual content(String(setReps))
                }
                .body(width: 50)
                    .overlay(
                        RoundedRectangle(cornerRadius: 5)
                            .stroke(Colour.black, lineWidth: 1)
                    )
                    .padding(.backside, 5)
                    .padding(.horizontal, 30)
            }
            
        }
        
        
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles