gpt4 book ai didi

ios - SwiftUI 组合 : @Published is only available on properties of classes

转载 作者:行者123 更新时间:2023-12-01 18:34:19 24 4
gpt4 key购买 nike

我有以下代码,我收到消息错误:

'wrappedValue' is unavailable: @Published is only available onproperties of classes

//*
/**
Chat
Created on 29/07/2020
*/

import SwiftUI

let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

struct ConnectionView: View {
@ObservedObject var keyboardResponder = KeyboardResponder()
@ObservedObject var viewModel = ConnectionVM()
// @State var uuid1: String = ""
// @State var uuid2: String = ""
@State var authenticationDidFail: Bool = false

var body: some View {
return VStack {
WelcomeText()
LogoImage()
UUIDTextField(uuid: viewModel.uuid1)
UUIDTextField(uuid: viewModel.uuid2)
if authenticationDidFail {
Text("Information not correct. Try again.")
.offset(y: -10)
.foregroundColor(.red)
}
Button(action: {
print("Button tapped")
}) {
LoginButtonContent()
}
}
.padding()
.offset(y: -keyboardResponder.currentHeight*0.5)
}
struct WelcomeText : View {
var body: some View {
return Text("Welcome!")
.font(.largeTitle)
.fontWeight(.semibold)
.padding(.bottom, 20)
}
}
struct LogoImage : View {
var body: some View {
return Image("logo")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 150, height: 150)
.clipped()
.cornerRadius(150)
.padding(.bottom, 75)
}
}
struct UUIDTextField : View {
@Published var uuid: String
var body: some View {
return TextField("UUID", text: $uuid)
.padding()
.background(lightGreyColor)
.cornerRadius(5.0)
.padding(.bottom, 20)
}
}
struct LoginButtonContent : View {
var body: some View {
return Text("LOGIN")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.green)
.cornerRadius(15.0)
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ConnectionView()
}
}
我的问题是
如何通过参数将@Published var 传递给 subview 。
enter image description here
更新问题
如果我使用绑定(bind)而不是发布,我会收到此错误:

Cannot convert value of type 'String' to expected argument type 'Binding'


enter image description here

最佳答案

@Published只能用于类和子 View是一个结构。您应该使用 @Binding而不是 @Published将绑定(bind)变量传递给子 View .

struct UUIDTextField : View {
@Binding var uuid: String
var body: some View {
return TextField("UUID", text: $uuid)
.padding()
.background(lightGreyColor)
.cornerRadius(5.0)
.padding(.bottom, 20)
}
}
然后使用绑定(bind)到传递的参数。
UUIDTextField(uuid: $viewModel.uuid1)
UUIDTextField(uuid: $viewModel.uuid2)

关于ios - SwiftUI 组合 : @Published is only available on properties of classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63202555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com