首页 > 解决方案 > Swift:无法将任何视图的返回表达式转换为某个视图的返回类型

问题描述

我不知道我有什么错误:无法将任何视图的返回表达式转换为返回类型某个视图

我尝试了一些东西,但没有任何效果......我在网上一无所获

谢谢你的帮助...

    struct LoginView: View {

    @Binding var showCreateAccount: Bool

    @State private var email = ""
    @State private var password = ""
    @State private var formOffset: CGFloat = 0
    @State private var presentPasswordRecoverySheet = false


    var body: some View {

         VStack {

            VStack(spacing: 40) {

                Image("Logo")

                Text("Login").font(.title).bold()

                VStack {
                    RoundTextfield(value: self.$email, placeholder: "Email", icon: Image(systemName: "at"),
                                    onEditingChanged: { flag in withAnimation { self.formOffset = flag ? -150 : 0 } })

                    RoundTextfield(value: self.$password, placeholder: "Password", icon: Image(systemName: "lock"), isSecure: true,
                                    onEditingChanged: { flag in withAnimation { self.formOffset = flag ? -150 : 0 } })

                    BasicButton(text: "Login") {}
                }


                Button(action: { withAnimation(.spring() ) { self.showCreateAccount.toggle() } } )
                {
                  HStack { Text("Don't have an account? Sign up.").accentColor(Color.accentColor) }
                }


                Button(action: { self.presentPasswordRecoverySheet.toggle() } )
                {
                  HStack { Text("Forgot your password?").accentColor(Color.purple) }
                }
                .sheet(isPresented: self.$presentPasswordRecoverySheet)
                    { RecoverPasswordView(presentPasswordRecoverySheet: self.$presentPasswordRecoverySheet) }

            }
            .padding()
            .offset(y: self.formOffset)

        }
    }

这是我的 BasicButton 代码,但我认为这不是问题

    struct BasicButton: View {
    var text = "Next"
    var action: (()->()) = {}

    var body: some View {
      Button(action: {
        self.action()
      }) {
        HStack {
            Text(text)
                .bold()
                .frame(minWidth: 0, maxWidth: .infinity)
                .padding(.vertical)
                .accentColor(Color.white)
                .background(Color("accentColor"))
                .cornerRadius(30)
            }
        }
    }
}

struct BasicButton_Previews: PreviewProvider {
    static var previews: some View {
        BasicButton()
    }

当我删除创建帐户和忘记密码的两个按钮功能时,代码可以构建

标签: swiftswiftuiswift5.2

解决方案


I’ve been fiddling with the same error all day. There’s no issues when building/running normally. It's very strange. I even tried commenting out almost all of the code in my view except for one small piece of it and I’m still hitting a problem with .font(.title) . Amazingly, if I try running the same code in a new project, no problem. Also if I just comment out that line setting the font type the preview compiles and shows up.

And the errors being reported are basically non-sense, they’re not even on the right line.

So I guess the preview feature is just riddled with bugs still. Hopefully it improves soon. Feels like the initial rollout of Swift where people started adopting and using it faster than Apple was able to fix it. Or rather, Swift was probably released before the tooling was ready for it. Seems to be the same case here with SwiftUI.


推荐阅读