【问题标题】:Value of type 'ContentView' has no member flag Tapped' and Closure containing a declaration cannot be used with result builder 'ViewBuilder''ContentView' 类型的值没有成员标志 Tapped' 并且包含声明的闭包不能与结果生成器 'ViewBuilder' 一起使用
【发布时间】:2021-03-28 07:38:15
【问题描述】:

有人可以检查以下代码并至少使其零编译器错误。目前,它有两个编译器错误,分别是“'ContentView'类型的值没有成员标志已被点击”和“包含声明的闭包不能与结果生成器'ViewBuilder一起使用”。

谢谢

import SwiftUI

struct ContentView: View {
    @State private var calculusians = ["Bernhard Riemann", "Brook Taylor", "Colin Maclaurin", "Gottfried Wilhelm Leibniz", "Guillaume de l'Hôpital", "Isaac Newton", "Johann Bernoulli", "Joseph-Louis Lagrange", "Leonhard Euler"].shuffled()
    @State private var correctAnswer = Int.random(in: 0...2)
    
    @State private var score = 0
    @State private var alertTitle = ""
    @State private var showingAlert = false
    
    var body: some View {
        NavigationView {
            VStack  {
                ForEach(0..<3) { number in
                    Image(self.calculusians[number])
                        .border(Color.black, width: 1)
                        .onTapGesture {
                            self.flagTapped(number)
                            
                            
                        }
                }
                .navigationBarTitle(Text(calculusians[correctAnswer]))
                .actionSheet(isPresented: $showingAlert) {
                    ActionSheet(title: Text(alertTitle), message: Text("Your score is \(score)"), buttons: [.default(Text("Continue"))])
                }
            }
        }
        func flagTapped( tag: Int) {
            if tag == correctAnswer {
                // they were right!
                score += 1
                alertTitle = "Correct"
            } else {
                //they were wrong!
                score -= 1
                alertTitle = "Wrong"
            }
            
            showingAlert = true
        }
        
        func askQuestion() {
            calculusians.shuffled()
            correctAnswer = Int.random(in: 0...2)
        }
    }
}

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    将函数放在主体变量之外。

    struct ContentView: View {
        @State private var calculusians = ["Bernhard Riemann", "Brook Taylor", "Colin Maclaurin", "Gottfried Wilhelm Leibniz", "Guillaume de l'Hôpital", "Isaac Newton", "Johann Bernoulli", "Joseph-Louis Lagrange", "Leonhard Euler"].shuffled()
        @State private var correctAnswer = Int.random(in: 0...2)
        
        @State private var score = 0
        @State private var alertTitle = ""
        @State private var showingAlert = false
        
        var body: some View {
            NavigationView {
                VStack  {
                    ForEach(0..<3) { number in
                        Image(self.calculusians[number])
                            .border(Color.black, width: 1)
                            .onTapGesture {
                                self.flagTapped(tag: number)
                            }
                    }
                    .navigationBarTitle(Text(calculusians[correctAnswer]))
                    .actionSheet(isPresented: $showingAlert) {
                        ActionSheet(title: Text(alertTitle), message: Text("Your score is \(score)"), buttons: [.default(Text("Continue"))])
                    }
                }
            }
        }
        
        func flagTapped( tag: Int) {
            if tag == correctAnswer {
                // they were right!
                score += 1
                alertTitle = "Correct"
            } else {
                //they were wrong!
                score -= 1
                alertTitle = "Wrong"
            }
            
            showingAlert = true
        }
        
        func askQuestion() {
            calculusians.shuffled()
            correctAnswer = Int.random(in: 0...2)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      相关资源
      最近更新 更多