简体   繁体   English

如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议

[英]how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

hi i'm making a game but i can't get an if state to work嗨,我正在制作游戏,但如果 state 无法正常工作

now i added the full code of the program (contentView.swift)现在我添加了程序的完整代码(contentView.swift)

hope its more useful to help me希望它对我更有用

the code is made in xcode and the program is for macos该代码是在 xcode 中制作的,该程序适用于 macos

coded in xcode whit swiftui编码在 xcode 和 swiftui

plz do not use this code its copyrighted by me请不要使用此代码,其版权归我所有


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 just filing操作只是归档

the game is for mac os catalina and can be use for probobly all appels stuff that can have apps on it该游戏适用于 mac os catalina 并且可以用于可能所有可以在其上安装应用程序的 appels 东西

This is because in the body of the View you need to return a view.这是因为在 View 的body中,您需要返回一个视图。 You can't just perform calculations etc. like in a normal function.您不能像普通的 function 那样只执行计算等。

You can remove this code (which doesn't return a View and thus the compilation is failing):您可以删除此代码(它不返回视图,因此编译失败):

else if gameState == 1 {
    if self.rand() == self.collor {
        self.text = "you won"
        self.data = 1
    }
}

and place it in you button's action:并将其放在您按钮的操作中:

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.

相关问题 navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 请帮忙:类型'()'不能符合'View'; 只有结构/枚举/类类型可以符合协议 - Please help: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols 类型 &#39;() -&gt; ()&#39; 不能符合 &#39;View&#39;; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols 协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议 - Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols Swift 协议类型“XXX”的值不能符合“可识别”; 只有结构/枚举/类类型可以符合协议 - Swift Value of protocol type 'XXX' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM