![](/img/trans.png)
[英]navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols”
[英]how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
嗨,我正在制作游戲,但如果 state 無法正常工作
現在我添加了程序的完整代碼(contentView.swift)
希望它對我更有用
該代碼是在 xcode 中制作的,該程序適用於 macos
編碼在 xcode 和 swiftui
請不要使用此代碼,其版權歸我所有
import SwiftUI
struct ContentView: View {
@State private var sp = false
@State private var loaded = false
@State private var uname = ""
@State private var text = "input a number"
@State private var etext = ""
@State private var sats = ""
@State private var mbt = "select collor"
@State private var gameState = 0
@State private var data = 0
@State private var temp = 0
@State private var type = 0
@State private var collor = 0
@State private var intsats = 0
//@State private var password = ""
var body: some View {
VStack {
if sp == false {
Text("Alien entertaiment presentate\n\n")
Text("casino\n\n")
Button(action: {
self.sp = true
//self.loadEndings()
//self.load()
}) {
Text("Start")
}
Text("\n\n")
}else if sp == true && loaded == false {
TextField("user name", text: $uname)
//SecureField("pasword", text: $password)
Text(uname)
//Text(password)
Button(action: {
self.loaded = true
self.load()
}) {
Text("sign in")
}
}else if sp == true && loaded == true {
Text("\(data)")
Text("\n")
Text(etext)
if etext != "" {Text("\n")}
Text("\n")
//Text(text)
Text("\n")
if gameState == 0 {
MenuButton(mbt) {
Button("red", action: {self.mbt = "red"; self.collor = 1})
Button("black", action: {self.mbt = "red"; self.collor = 2})
}
TextField("a number", text: $sats)
Button(action: {
self.intsats = Int(self.sats) ?? 0
if self.intsats == 0 || self.intsats > self.data {
self.etext = "not valid number or you do not have enuf mony"
}else{
self.gameState = 1
}
}) {
Text("bet")
}
}else if gameState == 1
{
if self.rand() == self.collor
{ //Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
self.text = "you won"
self.data = 1
}
}
}
}
}
let defaults = UserDefaults.standard
func save()
{
defaults.set(self.data, forKey: uname)
}
func load()
{
self.data = defaults.integer(forKey: uname)
self.temp = data
if temp == 0 {
self.data = 86
}
}
func rand() -> Int
{
var type = 0
type = Int.random(in: 1..<3)
return type
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
操作只是歸檔
該游戲適用於 mac os catalina 並且可以用於可能所有可以在其上安裝應用程序的 appels 東西
這是因為在 View 的body
中,您需要返回一個視圖。 您不能像普通的 function 那樣只執行計算等。
您可以刪除此代碼(它不返回視圖,因此編譯失敗):
else if gameState == 1 {
if self.rand() == self.collor {
self.text = "you won"
self.data = 1
}
}
並將其放在您按鈕的操作中:
if gameState == 0 {
...
TextField("a number", text: $sats)
Button(action: {
self.intsats = Int(self.sats) ?? 0
if self.intsats == 0 || self.intsats > self.data {
self.etext = "not valid number or you do not have enuf mony"
} else {
self.gameState = 1
if self.rand() == self.collor { // <- move it here
self.text = "you won"
self.data = 1
}
}
}) {
Text("bet")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.