我有两个符合它们的协议和视图.一个视图编译,另一个不编译.这是一个bug还是语法发生了变化?

除了"不符合第二个错误的协议"之外,我还发现了这个错误.这可能与此有关吗?

enter image description here

协议

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

第二个不........

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

推荐答案

只需将第二个视图的init的参数名从createAction更改为creationAction,正如协议中声明的那样

Swift相关问答推荐

map 视图上的SwiftUI渐变折线

MyApp类型不符合协议App''''

按变换zoom 在UIView中不起作用

如何在每天午夜重置布尔值

background Task(.backgroundTask)不适用于MySQL

如何获取嵌套数组中项的路径以修改数组?

SwiftData:非法try 在不同上下文中的对象之间建立关系

可以将属性名称传递给 Swift 中的函数吗?

在SwiftUI中如何预览嵌套的ScrollView,以避免触发可刷新修饰符

避免 `(())` 显式指定 Void 类型的枚举关联值

TabView 无法在屏幕上正确显示

如何在 swiftui 中设置给定字符串类型日期的日期倒计时?

在 Swift 的异步上下文中,如何指示我想要函数的同步版本?

为什么 String.contains 在我导入 Foundation 时表现不同?

在 Swift 中为(递归)扩展提供默认实例参数

快速的 AES 加密

Swift 5.0 编译器无法导入使用 Swift 4.2.1 编译的模块

Swift 中 NSFetchRequest 的多个 NSPredicates?

iOS/Swift:如何检测 UITextField 上的touch 动作

如何将应用程序与 iOS 联系人应用程序集成?