Swift : cannot convert return expression of any view to return type some view

Viewed 2826

I don't know what I have this error : cannot convert return expression of any view to return type some view

I try some things but nothing nothing worked... And I found nothing on the web

Thanks for your help...

    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)

        }
    }

This is my BasicButton code, but I think this is not the problem

    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()
    }

When I remove the two button function for create account and forgot password, the code can build

1 Answers

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.

Related