Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conforming to protocols – Bug?

I have two protocols and views that conform to them. One view compiles and the other does not. Could this be a bug or has the syntax changed?

I also get this error on top of "does not conform to protocol of the 2nd error" Could this be related?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Protocols

protocol RecipeComponent: CustomStringConvertible {
    init()
}

protocol ModifyComponentView: View {
    associatedtype Component
    init(component: Binding<Component>, creationAction: @escaping (Component) -> Void)
    
}

The first view compiles and conforms..

import SwiftUI

struct ModifyIngredientView: ModifyComponentView {
    
    @Binding var ingredient: Ingredient
    let createAction: ((Ingredient) -> Void)
    
    //@Environment(\.dismiss) private var dismiss
    
    init(component: Binding<Ingredient>, creationAction: @escaping (Ingredient) -> Void) {
        self._ingredient = component
        self.createAction = creationAction
    }
    
    var body: some View {
        

        VStack{
            Form{
                TextField("Ingredient", text: $ingredient.name)
                Stepper(value: $ingredient.quantity, in: 1...100, step: 0.5) {
                    HStack{
                        Text("Quantity:")
                        TextField("Quantity", value: $ingredient.quantity, formatter: NumberFormatter.decimal)
                            .keyboardType(.numbersAndPunctuation)
                    }
                }
                Picker(selection: $ingredient.unit) {
                    ForEach(Ingredient.Unit.allCases, id: \.self) {unit in
                        Text(unit.rawValue)
                    }
                } label: {
                    HStack {
                        Text("Unit")
                        Spacer()
                        //Text(ingredient.unit.rawValue)
                    }
                }
                .pickerStyle(DefaultPickerStyle())
                HStack {
                    Spacer()
                    Button("Save") {
                        createAction(ingredient)
                        //dismiss()
                    }
                    Spacer()
                }
                

                }
        
            }
        }
    }

extension NumberFormatter {
    static var decimal: NumberFormatter {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        return formatter
    }
}

struct ModifyIngredientView_Previews: PreviewProvider {
    
    @State static var emptyIngredient = Ingredient()
    
    static var previews: some View {
        NavigationView {
            ModifyIngredientView(component: $emptyIngredient) { ingredient in
                print(ingredient)
            }
        }.navigationTitle("Add Ingredient")
    }
}

The 2nd does not……..

import SwiftUI

struct ModifyDirectionView: ModifyComponentView {
    
    @Binding var direction: Direction
    let createAction: ((Direction) -> Void)
    
    init(component: Binding<Direction>, createAction: @escaping (Direction) -> Void) {
        self._direction = component
        self.createAction = createAction
    }

    private let listBackgroundColor = AppColor.background
    private let listTextColor = AppColor.foreground

    //@Environment(\.presentationMode) private var mode
    
    var body: some View {
        Form {
            TextField("Direction Description", text: $direction.description)
                .listRowBackground(listBackgroundColor)
            Toggle("Optional", isOn: $direction.isOptional)
                .listRowBackground(listBackgroundColor)
            HStack {
                Spacer()
                Button("Save") {
                    createAction(direction)
                    //mode.wrappedValue.dismiss()
                }
                Spacer()
            }.listRowBackground(listBackgroundColor)
        }
        .foregroundColor(listTextColor)
    }
}

struct ModifyDirectionView_Previews: PreviewProvider {
    @State static var recipe = Recipe.testRecipes[0]

    static var previews: some View {
        NavigationView {
            ModifyDirectionView(component: $recipe.directions[0]) { direction in
                print(direction)
            }
        }
    }
}

>Solution :

just change the parameter name of the init of the second view from createAction to creationAction, as it is declared in the protocol

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading