swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.77k stars 10.29k forks source link

Logging Page doesnt work. Failed to produce diagnostic for expression error. #63819

Open L1mm0n opened 1 year ago

L1mm0n commented 1 year ago

Description

import SwiftUI

struct ContentView: View {
    @State var showingLoginScreen: Bool?
    @State private var username = ""
    @State private var password = ""
    @State private var wrongUsername = 0
    @State private var wrongPassword = 0

var body: some View {
    NavigationView{
        ZStack{
            Color.white
                .ignoresSafeArea()
            VStack{
                Text("LOGIN")
                    .font(.largeTitle)
                    .bold()
                    .padding()
                TextField("Usename", text: $username)
                    .padding()
                    .frame(width:300, height:50)
                    .background(Color.black.opacity(0.05))
                    .cornerRadius(10)
                    .border(.red, width: CGFloat(wrongUsername))

                SecureField("Password", text: $password)
                    .padding()
                    .frame(width:300, height:50)
                    .background(Color.black.opacity(0.05))
                    .cornerRadius(10)
                    .border(.red, width: CGFloat(wrongUsername))

                Button("Login") {
                    autheticateUser(username: username, password: password)
                }
                .foregroundColor(.white)
            .frame(width: 300, height: 50)
                .background(Color.blue)
                .cornerRadius(10)

                NavigationLink(
                    destination: Text("Welcome@\(username)"),
                    isActive: showingLoginScreen != nil ? $showingLoginScreen! : nil
                ) {
                    EmptyView()
                }

            }
        }
    }
    .navigationBarHidden(true)
}

    func autheticateUser(username: String, password: String) {
        if username.lowercased() == "kevin" {
            wrongUsername = 0
            if password.lowercased() == "Kt200817" {
                wrongPassword = 0
                showingLoginScreen = true
            } else {
                wrongPassword = 1
            }
        } else {
            wrongUsername = 1
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project

Expected behavior I excepted it would navigate to the welcome page after entering the write username and password but it has this error.

xedin commented 1 year ago

The problem is with this line - isActive: showingLoginScreen != nil ? $showingLoginScreen! : nil

L1mm0n commented 1 year ago

Can you tell me exactly why? Because it will say showingLogingScreen as a bool, that why I do that line .

xedin commented 1 year ago

isActive: requires Binding<Bool> but $showingLoginScreen would produce Binding<Bool?> so ternary cannot be used here, the binding needs to be constructed explicitly using Binding(get:set:) initializer or drop optionality from showingLoginScreen declaration.