我该如何修复类型'()'不符合'视图';只有struct / enum / class类型可以符合协议

时间:2020-08-13 18:01:22

标签: swift swiftui macos-catalina

嗨,我在做游戏,但是我无法获得一个if状态

现在我添加了程序的完整代码(contentView.swift)

希望它对我有帮助

该代码是用xcode编写的,该程序适用于macos

以xcode swiftui编码

plz请勿使用此代码归我所有


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物品

1 个答案:

答案 0 :(得分:0)

这是因为在视图的body中,您需要返回一个视图。您不能像 normal 函数那样执行计算等。

您可以删除以下代码(该代码不会返回View,因此编译失败):

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")
    }
}