我可以以某种方式将 Binding<String> 转换为 String 吗?

Vla*_*cea 1 ios swift swiftui

我正在尝试调用一个如下所示的函数

func customTextField(placeholder: String, text: Binding<String>) -> some View {
        ZStack(alignment: .leading){
            if text.isEmpty{
                VStack {
                    Text(placeholder)
                        .font(.system(size: 14))
                        .fontWeight(.regular)
                        .foregroundColor(Color(red: 0.5803921568627451, green: 0.5803921568627451, blue: 0.5803921568627451))
                        .padding()
                    
                    if placeholder == "Your message type here..."{
                        Spacer()
                    }
                }
            }
            
            TextField("", text: text)
                .font(.system(size: 14))
                .foregroundColor(.black)
                .padding()
            
        }
        .frame(height: placeholder == "Your message type here..." ? 170 : 54)
        .background(RoundedRectangle(cornerRadius: 16).stroke(text.isEmpty ? Color.clear : Color("orange")))
        .background(Color.white)
        .cornerRadius(16)
    }
Run Code Online (Sandbox Code Playgroud)

但因为textBinding<String>我不能使用.isEmpty()它。我不想为它创建另一个结构视图。对此最好的方法是什么?

Dáv*_*tor 13

您只需调用wrappedValueaBinding即可访问其基础值,无需任何类型转换。

也如此

text.wrappedValue.isEmpty
Run Code Online (Sandbox Code Playgroud)

代替

text.isEmpty
Run Code Online (Sandbox Code Playgroud)