首页 > 解决方案 > 调用 SwiftUI 中位置 #11、#12 的额外参数

问题描述

我在 SwiftUI 中的切换开关上不断收到“位置 #11、#12 的额外参数”错误。我见过其他人有“Extra Arguments in call”错误,但答案似乎没有帮助;另外,我的错误是“位置#11、12”,我没有看到其他人发生这种情况。如果有区别,我正在使用 Xcode 12 beta。

import SwiftUI

let defaults = UserDefaults.standard

let notifsEnabled = defaults.bool(forKey: "NotifsEnabled")




struct Settings: View {
    @State var class11: String = defaults.string(forKey: "class11") ?? ""
    @State var class12: String = defaults.string(forKey: "class12") ?? ""
    @State var class13: String = defaults.string(forKey: "class13") ?? ""
    @State var class14: String = defaults.string(forKey: "class14") ?? ""
    
    @State var class21: String = defaults.string(forKey: "class21") ?? ""
    @State var class22: String = defaults.string(forKey: "class22") ?? ""
    @State var class23: String = defaults.string(forKey: "class23") ?? ""
    @State var class24: String = defaults.string(forKey: "class24") ?? ""
    
    @State var scheduleNotifications = notifsEnabled
    
    
    
    var body: some View {
        
        VStack(alignment: .leading) {
            
            Toggle(isOn: $scheduleNotifications) { //Extra arguments at positions #11, #12 in call
                Text("Daily schedule notifications")
            }
            
            if scheduleNotifications {
                Text(CreateNotifs())
            } else {
                Text(DeleteNotifs())
            }
            
            
            
            Text("This App will send you a reminder each day at 8:25 with the schedule for that day")
                .font(.caption)
                .foregroundColor(Color.gray)
            
            
            Divider()
            
            TextField("Class 1-1", text: $class11)
            
            TextField("Class 1-2", text: $class12)
            
            TextField("Class 1-3", text: $class13)
            
            TextField("Class 1-4", text: $class14)
            
            TextField("Class 2-1", text: $class21)
            
            TextField("Class 2-2", text: $class22)
            
            TextField("Class 2-3", text: $class23)
            
            TextField("Class 2-4", text: $class24)
            
            
            
            //Spacer()
        }
        .padding()
        .navigationBarTitle("Settings")
        
    }
    
}






标签: swiftswiftui

解决方案


ViewBuilder 在一个容器中仅支持不超过 10 个静态视图...这是您出错的原因

只是将它们分组

Group {

    TextField("Class 1-1", text: $class11)
    
    TextField("Class 1-2", text: $class12)
    
    TextField("Class 1-3", text: $class13)
    
    TextField("Class 1-4", text: $class14)
    
    TextField("Class 2-1", text: $class21)
    
    TextField("Class 2-2", text: $class22)
    
    TextField("Class 2-3", text: $class23)
    
    TextField("Class 2-4", text: $class24)

}

推荐阅读