Home > Mobile >  Type () cannot confirm to view
Type () cannot confirm to view

Time:04-17

I want to sort the totalWeights array based on the descendingDays bool (set in another view) and before the ForEach (without this 'sorting code' all works fine).

I get an error "Type '()' cannot conform to 'View'". How can I solve this?

struct ActualWeightsView: View {
    @Binding var descendingDays: Bool
    var egg: Egg
    var body: some View {
        var totalWeights = egg.actualWeights
        let count = totalWeights.reduce(0) { $1.measured ? $0   1 : $0 }
       
        if descendingDays { totalWeights = totalWeights.sorted { $0.day > $1.day } }
        else { totalWeights = totalWeights.sorted { $0.day > $1.day } }

        VStack {
            HStack(alignment: .lastTextBaseline) {
                Text("Egg weights")
                    .font(.title2)
                    .padding(.top, 40)
                Spacer()
                Text("\(count) measurements")
                    .font(.callout)
                    .foregroundColor(.secondary)
            }
            
            ForEach(totalWeights, id: \.self) { totalWeight in
                HStack(alignment: .center) {
                    if totalWeight.measured {
                        Text("Day: \(totalWeight.day)")
                        .padding(10)
                        .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 10))
                        .foregroundColor(.green)
                    } else {
                        Text("Day: \(totalWeight.day)")
                        .padding(10)
                        .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 10))
                        .foregroundColor(.red)
                    }
                    Spacer()
                    Text("Weight: \(formatVar(getal: totalWeight.weight))")
                    Spacer()
                    Text("@26: \(formatVar(getal: totalWeight.prediction)) %")
                        .multilineTextAlignment(.trailing)
                }
                .padding(.vertical)
            }
        }
    }
}

CodePudding user response:

You can't put imperative code like your if descending days { ... inside a View's body. You can only use variable assignments and view hierarchy.

To solve this, you can move totalWeights outside the body and make it a computed property. Note that I'm guessing about the type, since you didn't include the definition for Egg:

var totalWeights : [Double] {
    var totalWeights = egg.actualWeights
    let count = totalWeights.reduce(0) { $1.measured ? $0   1 : $0 }
   
    if descendingDays { totalWeights = totalWeights.sorted { $0.day > $1.day } }
    else { totalWeights = totalWeights.sorted { $0.day > $1.day } }
    return totalWeights
}

Lastly, although not directly related to your question, you should be careful about using id: \.self in your ForEach. Unless your IDs are uniquely-identifiable, you risk a crash or other unexpected behavior.

  • Related