首页 > 解决方案 > 'Int' 不能转换为 'CGFloat' Swift UI

问题描述

请帮助我不知道为什么我一直得到这个。我在 2 的底部一直得到错误。我不认为我可以编辑它。如果有人可以帮助我,那就太好了。编辑:添加了所有代码。谢谢你帮助我

(别介意,它告诉我要添加更多细节,welp)

import SwiftUI
import Foundation

struct ContentView: View {
 @State private var Waifus = ["Rem","Chika","Zero Two","SpeedWagon"].shuffled()
@State private var CorrectGirl = Int.random(in: 0...2)

@State private var ShowingScore = false
@State private var ScoreTitle = ""
@State private var Points = 0

var body: some View {
    ZStack {
        .background(Color.black)

        VStack(spacing: 30) {

            VStack {
                Spacer()
                Text("Tap The Waifu")
                    .foregroundColor(.white)
                    .font(.largeTitle)
                Text(Waifus[CorrectGirl])
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .fontWeight(.black)

            }
            ForEach(0 ..< 4 ) { number in
                Button(action: {
                    self.WaifuTapped(number)
                }) {
                    Image(self.Waifus[number])
                        .renderingMode(.original)
                        .frame(width: 88, height: 88)
                    .clipShape(Circle())
                        .overlay(Circle().stroke(Color.white, lineWidth : 4))



                }
            }
            Text("You have")
                .font(.largeTitle)
                .foregroundColor(.white)
            Text("\(Points) Points")
                .foregroundColor(.white)
                .font(.largeTitle)
                .fontWeight(.black)

            Spacer()
        }
    }
    .alert(isPresented: $ShowingScore) {
        Alert(title: Text(ScoreTitle),message: Text("Your Score is \(Points)"),dismissButton:
            .default(Text("Countinue Weeb")) {
                self.askQuestion()
            })
    }
}
func WaifuTapped(_ number : Int) {
    if number == CorrectGirl {
        ScoreTitle = "Correct"
        Points += 1
    } else {
        ScoreTitle = "Wrong, The Waifu was \(self.Waifus[number])"
    }

    ShowingScore = true
}

func askQuestion() {
    Waifus.shuffle()
    CorrectGirl = Int.random(in: 0...2)

}

}

标签: swiftswiftui

解决方案


好的,我将只显示body错误所在的位置:

// ... variables and other
    var body: some View {
        ZStack {
//            .background(Color.black) // this is the wrong line of code
//            it should be after "{" after ZStack
//            sure, you can use just "Rectangle()"

            VStack(spacing: 30) {

                VStack {
                    Spacer()
                    Text("Tap The Waifu")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                    Text(Waifus[CorrectGirl])
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .fontWeight(.black)

                }
                ForEach(0 ..< 4 ) { number in
                    Button(action: {
                        self.WaifuTapped(number)
                    }) {
                        Text("")
                        Image(self.Waifus[number])
                            .renderingMode(.original)
                            .frame(width: 88, height: 88)
                            .clipShape(Circle())
                            .overlay(Circle().stroke(Color.white, lineWidth : 4))



                    }
                }
                Text("You have")
                    .font(.largeTitle)
                    .foregroundColor(.white)
                Text("\(Points) Points")
                    .foregroundColor(.white)
                    .font(.largeTitle)
                    .fontWeight(.black)

                Spacer()
            }
        }
        .background(Color.black) // it should be here
        .alert(isPresented: $ShowingScore) {
            Alert(title: Text(ScoreTitle),message: Text("Your Score is \(self.Points)"),dismissButton:
                .default(Text("Countinue Weeb")) {
                    self.askQuestion()
                })
        }
    }

正如我在评论中所说:SwiftUI 中的错误显示在错误的位置。避免它们的建议:将您的视图提取body到其他结构中,并且应该更容易处理所有事情

更新这是唯一的错误,现在它可以工作了。我认为您只需要替换.background(..Rectangle(). 我只是给你指出了错误的代码行。

在此处输入图像描述


推荐阅读