对成员'buildBlock()'的歧义引用 [英] Ambiguous reference to member 'buildBlock()'

查看:91
本文介绍了对成员'buildBlock()'的歧义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Swift UI为iOS 13创建一个应用程序,但我不断收到这个奇怪的错误:对成员'buildBlock()'的引用不明确."

I've been trying to make an app for iOS 13 using Swift UI but I keep getting this weird error: "ambiguous reference to member 'buildBlock()'".

无论我做什么,错误都不会消失.

No matter what I do the error won't go away.

我尝试一次注释部分代码,以查看可能是引起问题的部分,但唯一可行的方法是注释掉整个视图.

I tried commenting of sections of code at a time to see which part might have been causing the issue, but the only thing that ever worked was commenting out the entire view.

我尝试清理构建文件夹并删除派生数据.我还尝试过多次重新启动计算机和Xcode,没有任何修复措施.

I've tried cleaning my build folder and deleting derived data. I also tried restarting my computer and Xcode multiple times, nothing has fixed it.

我很确定这只是一个Xcode错误,但是必须解决它,如果有人能告诉我这是什么,我将非常感谢.

I'm pretty sure that this is just an Xcode bug, but there must be away around it, I'd really appreciate if anyone could tell me what that is.

var body: some View {
    GeometryReader { geometry in {
        VStack {
            Button (action: self.editProfilePicture) {
                    Image(ImageNames.AccountIconImageName, bundle: Bundle.main)
                        .resizable()
                        .frame(width: geometry.size.width / SizeConstants.AccountIconSizeDiviser, height: geometry.size.width / SizeConstants.AccountIconSizeDiviser)
                        .padding()
                        .background(ColorConstants.VeryLightGrayColor)
                        .clipShape(Circle())
                }
                .accentColor(.white)
                .padding(.bottom, LargePadding)

                ScrollView (showsVerticalIndicator: false) {

                    let const: Length? = geometry.size.width - SizeConstants.FullNameTextFieldWidthReduction

                    TextBox(textBinding: self.$fullName, placeHolder: Text(Strings.FullNameString), editChanged: self.fullNameChanged)/*.padding(.bottom, SmallPadding)*/.frame(width: const)
                    TextBox(textBinding: self.$username, placeHolder: Text(Strings.UsernameString), editChanged: self.usernameChanged)//.padding(.bottom)

                    Text(verbatim: Strings.ChooseIdType).font(.footnote).color(.gray)

                    TextBox(textBinding: self.$phoneNumber, placeHolder: Text(Strings.PhoneNumberString), editChanged: self.phoneNumberChanged)//.padding(.bottom, SmallPadding)
                    TextBox(textBinding: self.$emailAddress, placeHolder: Text(Strings.EmailAddressString), editChanged: self.emailAddressChanged)//.padding(.bottom)

                    Spacer(minLength: PaddingConstants.FirstSignupSpacerMinSize)

                    TextBox(textBinding: self.$password, placeHolder: Text(Strings.PasswordFieldPlaceHolder), editChanged: self.signupPasswordChanged)//.padding(.bottom, SmallPadding)
                    TextBox(textBinding: self.$confirmPassword, placeHolder: Text(Strings.ConfirmPasswordString), editChanged: self.confirmPasswordChanged)//.padding(.bottom)

                    Spacer(minLength: PaddingConstants.SecondSignupSpacerMinSize)

                    Button (action: self.signup) {
                        Text(Strings.CreateAccountString).color(.white).font(Font.system(size: SizeConstants.LoginButtonFontSize))
                    }
                    .padding(EdgeInsets(top: PaddingConstants.CreatAccountButtonVerticalPadding,
                                        leading: PaddingConstants.CreateAccountButtonSidePadding,
                                        bottom: PaddingConstants.CreatAccountButtonVerticalPadding,
                                        trailing: PaddingConstants.CreateAccountButtonSidePadding))
                    .background(LeznoBlue)
                    .clipShape(RoundedRectangle(cornerRadius: SmallCornerRadius))

                    Spacer(minLength: PaddingConstants.ThirdSignupSpacerMinSize)

                    Text(Strings.AgreementString)

                    HStack {
                        Button (action: {}) {
                            Text(Strings.TermsString)
                        }
                        Text(Strings.AndString)
                        Button (action: {}) {
                            Text(Strings.PrivacyString)
                        }
                    }

                }
        }
        .padding()
    }
}

错误的屏幕截图

因此,事实证明我忘记了在视图构建器关闭中最多只能有10个视图,而我拥有的更多.只需将它们分组以减少视图计数即可解决该错误.

So as it turns out I forgot that you can only have a maximum of 10 view in a view builder closure, and I had more than that. Simply putting them into groups to reduce the view count solved the error.

事实证明,Xcode显示的错误极具误导性.

The error that Xcode displays is just very misleading as it turns out.

感谢Hamish指出

推荐答案

正如@Hamish在评论中最初提到的,ViewBuilder的子视图限制不能超过10个!因此,您应该将项目分成较小的部分,并尝试按组逐个添加.

As @Hamish originally mentioned in comments, ViewBuilder's can not exceeding over 10 subview limit! So you should group items in smaller pieces and try to add them group by group.

所以不是这样的:(这是一个基于原始代码的工作示例,不完全相同)

struct ContentView : View {

    @State var firstName: String = ""
    @State var lastName: String = ""

    @State var phoneNumber: String = ""
    @State var emailAddress: String = ""

    @State var password: String = ""
    @State var confirmPassword: String = ""

    var body: some View {

        ScrollView (showsVerticalIndicator: false) {

            TextField($firstName, placeholder: Text("First Name"))
            TextField($lastName, placeholder: Text("Last Name"))

            Spacer()

            TextField($phoneNumber, placeholder: Text("Phone Number"))
            TextField($emailAddress, placeholder: Text("Email Address"))

            Spacer()

            TextField($password, placeholder: Text("Password"))
            TextField($confirmPassword, placeholder: Text("Confirm Password"))

            // ...

        }
        .padding()
    }

}

考虑将其分成有意义的较小的小组,例如:

Consider breaking it into meaningful smaller groups like this:

struct NameSectionView : View {

    @State var firstName: String
    @State var lastName: String

    var body: some View {
        Group {
            TextField($firstName, placeholder: Text("First Name"))
            TextField($lastName, placeholder: Text("Last Name"))
        }
    }

}

struct ContactSectionView : View {

    @State var phoneNumber: String
    @State var emailAddress: String

    var body: some View {
        Group {
            TextField($phoneNumber, placeholder: Text("Phone Number"))
            TextField($emailAddress, placeholder: Text("Email Address"))
        }
    }

}

struct PasswordSectionView : View {

    @State var password: String
    @State var confirmPassword: String

    var body: some View {
        Group {
            TextField($password, placeholder: Text("Password"))
            TextField($confirmPassword, placeholder: Text("Confirm Password"))
        }
    }

}

并像这样使用它们:

struct ContentView : View {

    @State var firstName: String = ""
    @State var lastName: String = ""

    @State var phoneNumber: String = ""
    @State var emailAddress: String = ""

    @State var password: String = ""
    @State var confirmPassword: String = ""

    var body: some View {

        ScrollView (showsVerticalIndicator: false) {

            NameSectionView(firstName: firstName, lastName: lastName)

            Spacer()

            ContactSectionView(phoneNumber: phoneNumber, emailAddress: emailAddress)

            Spacer()

            PasswordSectionView(password: password, confirmPassword: confirmPassword)

            // ...

        }
        .padding()
    }

}

如果您想在其他任何地方使用这些工具中的任何一个,这也更可重用.

Also this is more reusable if you ever want to use any of these somewhere else.

这篇关于对成员'buildBlock()'的歧义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆