SwiftUI出现问题,未在画布上显示iPhone XR预览。当我构建代码时,它可以正常工作,并且即使在iPhone模拟器上也可以成功显示,但是在Xcode中,它显示错误消息“无法构建ContentView.Swift”,并显示“编译失败: #sourceLocation指令结尾” 我尝试搜索解决方案并将其粘贴到我的终端机中
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
然后运行
sudo xcodebuild -license
同意许可并尝试 Xcode>首选项>位置>将命令行工具更改为Xcode 11.1 但似乎都无法解决问题。 这是错误外观的照片。 Failed to Build ContentView.Swift。有谁知道我该怎么做才能解决此问题? 如果重要的话,我的Xcode版本是11.1,下面是我的ContentView代码。
编辑说明:对不起,我是编程新手,忘了包括我尝试过的内容和源代码信息!如果有人可以帮助我,我将不胜感激。
import SwiftUI
struct ContentView: View {
@State var alertIsVisible = false
@State var sliderValue = 50.0
@State var target = Int.random(in: 1...100)
var body: some View {
VStack {
Spacer()
// Target row
HStack {
Text("Put the bullseye as close as you can to:")
Text("\(target)")
}
Spacer()
// Slider Row
HStack{
Text("1")
Slider(value: $sliderValue, in: 1...100)
Text("100")
}
Spacer()
//Button row
Button(action: {
self.alertIsVisible = true
}) {
Text(/*@START_MENU_TOKEN@*/"Hit Me!"/*@END_MENU_TOKEN@*/)
}
.alert(isPresented: $alertIsVisible) { () -> Alert in
return Alert(title: Text("Hello There!"),
message: Text("The slider's value is \(sliderValueRounded()).\n" +
"You scored \(pointsForCurrentRound()) points this round."),
dismissButton: .default(Text("Awesome!")))
}
Spacer()
// Score Row
HStack{
Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
Text("Start over")
}
Spacer()
Text("Score: ")
Text("999999")
Spacer()
Text("Round: ")
Text("999")
Spacer()
Button(action: {}){
Text("Info")
}
}
.padding(.bottom, 20)
}
}
func sliderValueRounded() -> Int {
Int(sliderValue.rounded())
}
func pointsForCurrentRound() -> Int {
100 - abs(target - sliderValueRounded())
}
}
struct ContentView_Previews:
PreviewProvider {
static var previews: some View {
ContentView().previewLayout(PreviewLayout.fixed(width: 896, height: 414))
}
}