表达式类型不明确,没有更多上下文xcode 11

我需要帮助。 我试图做一个检查数字是否为素数的应用程序。每次我尝试运行它都会给我带来错误。 我试图添加文本字段以获取输入。 你能帮我得到更好的设计吗?我一直在寻找使它看起来很酷的颜色。 我的代码:

var number:Int = 56
var isPrime:Bool = true
import UIKit
import SwiftUI
var x = "is it prime?"
struct ContentView: View {
@State private var x = "is it prime?"


    var body: some View {
        ZStack{
                    Color.red
            HStack {
                TextField("Placeholder",text: number)
                Button(action: {switch number {

                    case 1:
                        isPrime = false

                    case 2:
                        isPrime = true

                    case 3:
                        isPrime = true

                    default:
                        primeCheck:for i in 2...Int(sqrt(Double(number))) {

                            if number % i == 0 {

                                isPrime = false
                                break primeCheck

                            }
                        }

                    }

                    if isPrime {

                        self.x = "The number \(number) is prime!"

                    } else {

                        self.x = "The number \(number) is composite!"
                    }}) {
                     Text("\(x)")
                    .frame(width:300,height:100)
                    .padding()
                    .background(Color.gray)
                    .cornerRadius(40)
                    .foregroundColor(.white)
                }
            }
    }
}
}
linda881633 回答:表达式类型不明确,没有更多上下文xcode 11

您需要一个@State,以便UI根据变量x

更新

您还应该在体内将变量移动,因为不需要将该变量作为实例变量。

struct ContentView: View {
    @State private var x = 1

    var body: some View {
        VStack {
            Button(action: {
                self.x = 2
            }) {
                Text("increase")
            }
            Text("\(x)")
        }
    }
}
本文链接:https://www.f2er.com/3135263.html

大家都在问