SwiftUI:随机“调用中的额外参数”错误

Posted

技术标签:

【中文标题】SwiftUI:随机“调用中的额外参数”错误【英文标题】:SwiftUI: Random "Extra argument in call" error 【发布时间】:2020-07-25 11:30:35 【问题描述】:

所以我正在尝试学习 SwiftUI 和 Combine。我通常通过制作一个简单的小费计算器来开始新技术。

我似乎得到了一个随机的“调用中的额外参数”。编码时出错 这是我的 SwiftUI 文件

import SwiftUI

internal enum ReceiptRowType 
    case subtotal
    case tax
    case total
    case tip
    case grandTotal


struct TipView: View 
    @ObservedObject internal var adBannerView: BannerAdView = BannerAdView()
    @ObservedObject internal var receiptViewModel: ReceiptViewModel

    private let percentageFormatter: NumberFormatter = 
        let f = NumberFormatter()
        f.numberStyle = .percent
        return f
    ()

    var body: some View 
        ZStack 
            Color.white
                .scaledToFit()

            VStack 
                if adBannerView.adHasLoaded 
                    adBannerView
                        .frame(maxHeight: adBannerView.adHeight)
                        .animation(.easeInOut(duration: 2.0))
                

                BorderView()

                Text(ARCHLocalizedStrings.receipt)
                    .foregroundColor(Color.gray)

                BorderView()

                HStack 
                    Spacer()

                    Button(action: 
                        self.receiptViewModel.addNewReceiptItem()
                    ) 
                        Text(ARCHLocalizedStrings.buttonTitleAddItem)
                    
                

                BorderView()

                ScrollView 
                    ForEach(receiptViewModel.receiptItems)  receiptItem in
                        ItemView(receiptItem: receiptItem)

                        if receiptItem != self.receiptViewModel.receiptItems.last 
                            Divider()
                        
                    
                

                BorderView()

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.subtotal,
                                   title: ARCHLocalizedStrings.subtotal)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.tax,
                                   title: ARCHLocalizedStrings.tax)
            
            .padding(.horizontal, ARCHSwiftUILayoutConstants.defaultPaddingAndSpacing)
        
    


struct BorderView: View 
    var body: some View 
        Text("================================")
            .lineLimit(1)
            .foregroundColor(Color.gray)
            .minimumScaleFactor(0.5)
    


struct ItemView: View 
    @ObservedObject var receiptItem: ReceiptItemViewModel

    var body: some View 
        HStack 
            TextField(receiptItem.name, text: $receiptItem.name)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .foregroundColor(Color.gray)
                .multilineTextAlignment(TextAlignment.leading)

            TextField("Price", value: $receiptItem.price, formatter: ARCHUtilities.currencyFormatter)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .foregroundColor(Color.gray)
                .multilineTextAlignment(TextAlignment.trailing)
                .minimumScaleFactor(0.5)
                .frame(width: ARCHSwiftUILayoutConstants.widthForCurrency)
        
    


struct BottomOfReceiptRow: View 
    @ObservedObject internal var receiptViewModel: ReceiptViewModel

    internal var type: ReceiptRowType
    internal var title: String

    var body: some View 
        HStack 
            Spacer()

            Text(title)
                .foregroundColor(Color.gray)

            if type == ReceiptRowType.subtotal 
                Text("\(receiptViewModel.subtotal)")
                    .foregroundColor(Color.gray)
                    .frame(width: ARCHSwiftUILayoutConstants.widthForCurrency)
             else if type == ReceiptRowType.tax 
                Text("\(receiptViewModel.taxRate)")
                    .foregroundColor(Color.gray)
                    .frame(width: ARCHSwiftUILayoutConstants.widthForCurrency)
             else if type == ReceiptRowType.total 
                Text("\(receiptViewModel.total)")
                    .foregroundColor(Color.gray)
                    .frame(width: ARCHSwiftUILayoutConstants.widthForCurrency)
             else if type == ReceiptRowType.tip 

             else if type == ReceiptRowType.grandTotal 
                Text("\(receiptViewModel.grandTotal)")
                    .foregroundColor(Color.gray)
                    .frame(width: ARCHSwiftUILayoutConstants.widthForCurrency)
            
        
    


struct TipView_Previews: PreviewProvider 
    static var previews: some View 
        TipView(receiptViewModel: ReceiptViewModel())
    

但是,如果我在 TipView 主体(任何视图)上添加另一个视图,我似乎会收到“调用中的额外参数”错误。

Picture of error here

有人知道发生了什么吗?

【问题讨论】:

尝试围绕您的观点创建一个群组 。 Swiftui 中只允许使用 10 个……使用组可以添加更多。或使用子视图...(也将是更简洁的代码) 这个可行,我不知道有限制。但不是一个小组(小组工作完美),我可能应该把一些事情撕成他们自己的观点。 Thnx 请把它写成答案,我会把它作为官方答案。 Chris 和 SparkleBeard 都是正确的解决方案。 【参考方案1】:

尝试围绕您的观点创建一个群组 。 Swiftui 中只允许使用 10 个……使用组可以添加更多。或使用子视图...(也将是更简洁的代码)

【讨论】:

Swiftui 中只允许使用 10 个。这真的很有帮助。谢谢! 您说得对。感谢您兑现我为解决此错误所花费的一整天时间。 感谢 Apple 的另一个随机限制......现在我的代码有 11 个视图,为了“使代码简单易读”有 22 个额外的包装行! - 感谢您解决这个问题。当我注意到这个错误出现在第 11 个视图时,我立即怀疑另一个随机限制......【参考方案2】:

SwiftUI 中的@ViewBuilder 系统在任何给定的视图容器中都被限制为 10 个视图。第 11 个视图没有任何论据,因此您会收到该错误。

基本解决方案:

这里的问题是您的 VStack 已达到最大容量,但是您可以将这些现有视图包装在其他容器中。作为一个非常基本的示例,它允许您显示 10 个BottomReceiptRow 视图:

    var body: some View 
    ZStack 
        Color.white
            .scaledToFit()

// Modifications start here

        VStack 
            if adBannerView.adHasLoaded 
                adBannerView
                    .frame(maxHeight: adBannerView.adHeight)
                    .animation(.easeInOut(duration: 2.0))
            

            BorderView()

            Text(ARCHLocalizedStrings.receipt)
                .foregroundColor(Color.gray)

            BorderView()

            HStack 
                Spacer()

                Button(action: 
                    self.receiptViewModel.addNewReceiptItem()
                ) 
                    Text(ARCHLocalizedStrings.buttonTitleAddItem)
                
            

            BorderView()

            ScrollView 
                ForEach(receiptViewModel.receiptItems)  receiptItem in
                    ItemView(receiptItem: receiptItem)

                    if receiptItem != self.receiptViewModel.receiptItems.last 
                        Divider()
                    
                
            

            BorderView()

            Group 
                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.subtotal,
                                   title: ARCHLocalizedStrings.subtotal)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.tax,
                                   title: ARCHLocalizedStrings.tax)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.someValue,
                                   title: ARCHLocalizedStrings.someValue)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.someValue2,
                                   title: ARCHLocalizedStrings.someValue2)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.someValue3,
                                   title: ARCHLocalizedStrings.someValue3)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.someValue4,
                                   title: ARCHLocalizedStrings.someValue4)

                BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                                   type: ReceiptRowType.someValue5,
                                   title: ARCHLocalizedStrings.someValue5)
            
// Modifications end here

        
        .padding(.horizontal, ARCHSwiftUILayoutConstants.defaultPaddingAndSpacing)
    

或者:

您可能更愿意将这些部分完全重构为它们自己的View,因为组合使代码更易于阅读。不过,这里并不是绝对必要的。

如果您确实想这样做,您可以考虑一个示例,其中这些收据行都在它们自己的视图中,例如:

    var body: some View 
    ZStack 
        Color.white
            .scaledToFit()

        VStack 
            if adBannerView.adHasLoaded 
                adBannerView
                    .frame(maxHeight: adBannerView.adHeight)
                    .animation(.easeInOut(duration: 2.0))
            

            BorderView()

            Text(ARCHLocalizedStrings.receipt)
                .foregroundColor(Color.gray)

            BorderView()

            HStack 
                Spacer()

                Button(action: 
                    self.receiptViewModel.addNewReceiptItem()
                ) 
                    Text(ARCHLocalizedStrings.buttonTitleAddItem)
                
            

            BorderView()

            ScrollView 
                ForEach(receiptViewModel.receiptItems)  receiptItem in
                    ItemView(receiptItem: receiptItem)

                    if receiptItem != self.receiptViewModel.receiptItems.last 
                        Divider()
                    
                
            

            BorderView()

            bottomRow
        
        .padding(.horizontal, ARCHSwiftUILayoutConstants.defaultPaddingAndSpacing)
    


// Additional computed property in TipView
var bottomRow: some View 
    Group 
        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.subtotal,
                           title: ARCHLocalizedStrings.subtotal)

        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.tax,
                           title: ARCHLocalizedStrings.tax)

        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.someValue,
                           title: ARCHLocalizedStrings.someValue)

        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.someValue2,
                           title: ARCHLocalizedStrings.someValue2)

        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.someValue3,
                           title: ARCHLocalizedStrings.someValue3)

        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.someValue4,
                           title: ARCHLocalizedStrings.someValue4)

        BottomOfReceiptRow(receiptViewModel: receiptViewModel,
                           type: ReceiptRowType.someValue5,
                           title: ARCHLocalizedStrings.someValue5)
    
 // end bottomRow

【讨论】:

以上是关于SwiftUI:随机“调用中的额外参数”错误的主要内容,如果未能解决你的问题,请参考以下文章

swift2:调用中的额外参数“错误”

MKPolygon 初始化错误“调用中的参数“interiorPolygons”缺少参数”/“调用中的额外参数”

Alamofire发布请求错误调用中的额外参数“方法”

Alamofire 请求收到错误“调用中的额外参数”

session.dataTask 调用错误中的 Swift5 额外参数“completionHandler”

Swift 游乐场错误:调用中的额外参数“超时”