我该如何修复类型 '()' 不符合 'View';只有 struct/enum/class 类型可以符合协议
how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
嗨,我正在制作游戏,但我无法让 if 状态正常工作
现在我添加了程序的完整代码(contentView.swift)
希望对我更有帮助
代码是在xcode中编写的,程序是针对macos
的
编码在 xcode whit 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()
}
}
ops 刚刚归档
该游戏适用于 mac os catalina,并且可以用于可能包含应用程序的所有应用程序
这是因为在 body
的视图中您需要 return 一个视图。您不能像在 normal 函数中那样只执行计算等。
您可以删除此代码(它没有 return 视图,因此编译失败):
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")
}
}
嗨,我正在制作游戏,但我无法让 if 状态正常工作
现在我添加了程序的完整代码(contentView.swift)
希望对我更有帮助
代码是在xcode中编写的,程序是针对macos
的编码在 xcode whit 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()
}
}
ops 刚刚归档
该游戏适用于 mac os catalina,并且可以用于可能包含应用程序的所有应用程序
这是因为在 body
的视图中您需要 return 一个视图。您不能像在 normal 函数中那样只执行计算等。
您可以删除此代码(它没有 return 视图,因此编译失败):
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")
}
}