gpt4 book ai didi

swift - 调用中参数 'message' 缺少参数

转载 作者:行者123 更新时间:2023-12-04 15:11:06 24 4
gpt4 key购买 nike

我带着另一个问题回来了。我正在遵循本指南:https://medium.com/@fs.dolphin/passing-data-between-views-in-swiftui-793817bba7b1

一切正常,但 SecondView_Previews 抛出错误 Missing argument for parameter 'message' in call。这是我的 ContentView 和 SecondView

// ContentView
import SwiftUI

struct ContentView: View {
@State private var showSecondView = false
@State var message = "Hello from ContentView"

var body: some View {
VStack {
Button(action: {
self.showSecondView.toggle()
}){
Text("Go to Second View")
}.sheet(isPresented: $showSecondView){
SecondView(message: self.message)
}
Button(action: {
self.message = "hi"
}) {
Text("click me")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI

struct SecondView: View {
@State var message: String

var body: some View {
Text("\(message)")
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView() // Error here: Missing argument for parameter 'message' in call.
}
}

它尝试将其更改为 SecondView(message: String) 并且错误更改为“无法将类型 'String.Type' 的值转换为预期的参数类型 'String'”

谁能解释一下我做错了什么,或者如何正确设置预览。没有预览时一切正常。提前致谢!

最佳答案

struct ContentView: View {
@State var message: String //Define type here
var body: some View {
Text("\(message)")
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(message: "Some text") //Passing value here
}
}

关于swift - 调用中参数 'message' 缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65188764/

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