View SwiftUI错误-Type()无法确认查看:只有struct/enum/class类型可以符合协议
我有一些简单的代码,通过调用一个函数(createSampleData)创建一个数组(newFactoid),然后将其作为状态保存。视图显示阵列的记录1。到现在为止,一直都还不错。但是,我试图在视图中插入一个按钮,该按钮调用一个简单函数(refreshFactoid),该函数洗牌数组,理论上导致视图刷新。问题是我在插入按钮/函数时遇到了上述错误。如果我移除按钮,错误就会消失。感谢您的指点/帮助View SwiftUI错误-Type()无法确认查看:只有struct/enum/class类型可以符合协议,view,swiftui,vstack,View,Swiftui,Vstack,我有一些简单的代码,通过调用一个函数(createSampleData)创建一个数组(newFactoid),然后将其作为状态保存。视图显示阵列的记录1。到现在为止,一直都还不错。但是,我试图在视图中插入一个按钮,该按钮调用一个简单函数(refreshFactoid),该函数洗牌数组,理论上导致视图刷新。问题是我在插入按钮/函数时遇到了上述错误。如果我移除按钮,错误就会消失。感谢您的指点/帮助 import SwiftUI struct ContentView : View { @S
import SwiftUI
struct ContentView : View {
@State private var newFactoid = createSampleData()
var body: some View {
VStack {
// Display Category
Text(newFactoid[1].category).fontWeight(.thin)
.font(.title)
// Display Image
Image("Factoid Image \(newFactoid[1].ref)")
.resizable()
.scaledToFit()
.cornerRadius(15)
.shadow(color: .gray, radius: 5, x:5, y:5)
.padding(25)
// Display Factoid
Text("A: \(newFactoid[1].fact)")
.padding(25)
.multilineTextAlignment(.center)
.background(Color.white)
.cornerRadius(15)
.shadow(color: .gray, radius: 5, x:5, y:5)
.padding(25)
// Display Odds
Text("B: \(newFactoid[1].odds)").fontWeight(.ultraLight)
.font(.title)
.padding()
.frame(width: 150, height: 150)
.clipShape(Circle())
.multilineTextAlignment(.center)
.overlay(Circle().stroke(Color.white, lineWidth: 2))
.shadow(color: .gray, radius: 5, x: 5, y: 5)
// Refresh Button
Button (action: {refreshFactoid()}) {
Text("Press To Refresh Data")
}
// Refresh Function
func refreshFactoid() {
newFactoid.shuffle()
}
} // End of VStack Closure
}
}
struct TextUIView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
移除
// Refresh Function
func refreshFactoid() {
newFactoid.shuffle()
}
} // End of VStack Closure
从
正文
中,无法在VStack中声明func
在按钮的操作块中触发洗牌:
按钮(操作:{self.newFactoid.shuffle()}){
文本(“按以刷新数据”)
}
或在视图结构中声明函数:
struct ContentView:View{
@国家私有变量newFactoid=createSampleData()
var body:一些观点{
VStack{
// ...
//刷新按钮
按钮(操作:{self.refreshFactoid()}){
文本(“按以刷新数据”)
}
}//VStack闭包结束
}
//刷新功能
func refreshFactoid(){
newFactoid.shuffle()
}
}
谢谢你,拉尔夫。工作完美。