You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode内容预览无法输入@符号的技术求助

问题:Xcode SwiftUI预览无法输入@符号

在Mac上使用Xcode的Content Preview测试带注册登录功能的SwiftUI App时,遇到以下问题:

  • 按下Option+Q输入的是œ符号,而非@
  • 从外部粘贴@符号也无法生效
  • 导致邮箱格式验证失败,无法完成登录/注册流程
解决方案
  • 检查键盘布局:确认Mac系统的键盘布局为美式英文(美式布局下@的快捷键是Option+2),部分欧洲键盘布局会将œ映射到Option+Q,导致输入错误。可通过「系统设置-键盘-输入源」切换布局。
  • 启用硬件键盘模拟:打开Xcode预览窗口,点击顶部菜单栏的「Editor」,勾选「Simulate Hardware Keyboard」,避免虚拟键盘的输入冲突。
  • 重置Xcode预览缓存:按下Command+Option+P刷新预览,或直接重启Xcode,解决预览进程的缓存异常。
  • 临时代码赋值测试:若仅需验证登录/注册逻辑,可直接在@State变量中预设带@的邮箱,比如:
    @State private var email: String = "test@example.com"
    
  • 确认预览设备类型:确保预览的是iOS设备,而非Mac Catalyst目标,后者的键盘输入处理可能存在兼容性问题。
相关代码
struct LoginView: View {
    @State private var email: String = ""
    @State private var password: String = ""
    @State private var isLoggedIn: Bool = false
    @State private var errorMessage: String = ""

    var body: some View {
        VStack(spacing: 20) {
            Text("My Project")
                .font(.largeTitle)
                .fontWeight(.bold)

            TextField("Email", text: $email)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .autocapitalization(.none)
                .keyboardType(.emailAddress)

            SecureField("Password", text: $password)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            if !errorMessage.isEmpty {
                Text(errorMessage)
                    .foregroundColor(.red)
                    .font(.footnote)
            }

            Button(action: {
                loginUser()
            }) {
                Text("Login")
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }

            NavigationLink("Don't have an account? Register here.", value: "Register")
                .font(.footnote)

            NavigationLink(value: "CategorySelection") {
                EmptyView()
            }
            .navigationDestination(for: String.self) { destination in
                if destination == "CategorySelection" {
                    CategorySelectionView()
                } else if destination == "Register" {
                    RegistrationView()
                }
            }
            .disabled(!isLoggedIn)
        }
        .padding()
    }

    private func loginUser() {
        guard !email.isEmpty, !password.isEmpty else {
            errorMessage = "Please enter both email and password."
            return
        }

        Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
            if let error = error {
                errorMessage = error.localizedDescription
            } else {
                errorMessage = ""
                isLoggedIn = true
            }
        }
    }
}

struct RegistrationView: View {
    @State private var email: String = ""
    @State private var password: String = ""
    @State private var errorMessage: String = ""
    @State private var isRegistered: Bool = false

    var body: some View {
        VStack(spacing: 20) {
            Text("Register")
                .font(.largeTitle)
                .fontWeight(.bold)

            TextField("Email", text: $email)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .autocapitalization(.none)
                .keyboardType(.emailAddress)

            SecureField("Password", text: $password)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            if !errorMessage.isEmpty {
                Text(errorMessage)
                    .foregroundColor(.red)
                    .font(.footnote)
            }

            Button(action: {
                registerUser()
            }) {
                Text("Register")
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.green)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }

            NavigationLink(value: "Login") {
                EmptyView()
            }
            .navigationDestination(for: String.self) { destination in
                if destination == "Login" {
                    LoginView()
                }
            }
            .disabled(!isRegistered)
        }
        .padding()
    }

    private func registerUser() {
        guard !email.isEmpty, !password.isEmpty else {
            errorMessage = "Please enter both email and password."
            return
        }

        Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
            if let error = error {
                errorMessage = error.localizedDescription
            } else {
                errorMessage = ""
                isRegistered = true // 注册成功后跳转至LoginView
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者László Bús

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:09:57