对成员 'buildBlock()' 的引用不明确
Ambiguous reference to member 'buildBlock()'
我一直在尝试使用 Swift UI 为 iOS 13 制作一个应用程序,但我一直收到这个奇怪的错误:"ambiguous reference to member 'buildBlock()'".
无论我做什么,错误都不会消失。
我尝试一次对代码段进行注释,以查看可能是哪部分导致了问题,但唯一有效的方法是注释掉整个视图。
我已经尝试清理我的构建文件夹并删除派生数据。我也尝试重新启动我的计算机并 Xcode 多次,没有任何修复它。
我很确定这只是一个 Xcode 错误,但一定有解决办法,如果有人能告诉我那是什么,我将不胜感激。
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()
}
}
Screenshot of the error
编辑:
事实证明,我忘记了在视图构建器闭包中最多只能有 10 个视图,而我拥有的不止于此。只需将它们分组以减少观看次数即可解决错误。
Xcode 显示的错误结果非常具有误导性。
感谢 Hamish 指出
正如@Hamish 最初在评论中提到的,ViewBuilder 不能超过 10 个子视图!因此,您应该将项目分成更小的部分,并尝试逐组添加它们。
所以不是这样的:(这是一个基于你的原始代码的工作示例,不完全相同)
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()
}
}
考虑像这样将其分成有意义的小组:
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()
}
}
此外,如果您想在其他地方使用其中的任何一个,它的可重用性更高。
我一直在尝试使用 Swift UI 为 iOS 13 制作一个应用程序,但我一直收到这个奇怪的错误:"ambiguous reference to member 'buildBlock()'".
无论我做什么,错误都不会消失。
我尝试一次对代码段进行注释,以查看可能是哪部分导致了问题,但唯一有效的方法是注释掉整个视图。
我已经尝试清理我的构建文件夹并删除派生数据。我也尝试重新启动我的计算机并 Xcode 多次,没有任何修复它。
我很确定这只是一个 Xcode 错误,但一定有解决办法,如果有人能告诉我那是什么,我将不胜感激。
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()
}
}
Screenshot of the error
编辑:
事实证明,我忘记了在视图构建器闭包中最多只能有 10 个视图,而我拥有的不止于此。只需将它们分组以减少观看次数即可解决错误。
Xcode 显示的错误结果非常具有误导性。
感谢 Hamish 指出
正如@Hamish 最初在评论中提到的,ViewBuilder 不能超过 10 个子视图!因此,您应该将项目分成更小的部分,并尝试逐组添加它们。
所以不是这样的:(这是一个基于你的原始代码的工作示例,不完全相同)
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()
}
}
考虑像这样将其分成有意义的小组:
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()
}
}
此外,如果您想在其他地方使用其中的任何一个,它的可重用性更高。