我在一个名为 QuizBrain.swift 的新文件中创建了一个结构“QuizBrain”,旨在在我的 ViewController.swift 文件中使用。但是,当我转到 View Controller 文件并将结构设置为等于变量 'var quizBrain = Quizbrain()' 时,该结构保持白色并且不会突出显示任何颜色,并且其所有元素在 View 中都无法访问 Controller 文件。我不知道为什么 View Controller 文件不会将其识别为结构。这是未完成 QuizBrain.swift 代码:
import Foundation
struct QuizBrain {
let quiz = [
Question(q: "A slug's blood is green.", a: "True"),
Question(q: "Approximately one quarter of human bones are in the feet.", a: "True"),
Question(q: "The total surface area of two human lungs is approximately 70 square metres.", a: "True"),
Question(q: "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", a: "True"),
Question(q: "In London, UK, if you happen to die in the House of Parliament, you are technically entitled to a state funeral, because the building is considered too sacred a place.", a: "False"),
Question(q: "It is illegal to pee in the Ocean in Portugal.", a: "True"),
Question(q: "You can lead a cow down stairs but not up stairs.", a: "False"),
Question(q: "Google was originally called 'Backrub'.", a: "True"),
Question(q: "Buzz Aldrin's mother's maiden name was 'Moon'.", a: "True"),
Question(q: "The loudest sound produced by any animal is 188 decibels. That animal is the African Elephant.", a: "False"),
Question(q: "No piece of square dry paper can be folded in half more than 7 times.", a: "False"),
Question(q: "Chocolate affects a dog's heart and nervous system; a few ounces are enough to kill a small dog.", a: "True")
]
var questionNumber = 0
func checkAnswer(_ userAnswer: String) {
if userAnswer == quiz[questionNumber].answer {
// User got it right
} else {
// User got it wrong
}
}
}
这是未完成的 View Controller 代码,其中包含尝试利用结构元素的函数:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var trueButton: UIButton!
@IBOutlet weak var falseButton: UIButton!
var quizBrain = QuizBrain()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
updateUI()
}
@IBAction func answerButtonPressed(_ sender: UIButton) {
let userAnswer = sender.currentTitle! // True or false
quizBrain.checkAnswer(userAnswer)
if userAnswer == actualAnswer {
sender.backgroundColor = UIColor.green
} else {
sender.backgroundColor = UIColor.red
}
if (questionNumber + 1 ) < quiz.count {
questionNumber += 1
} else {
questionNumber = 0
}
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
}
像 checkAnswer 和 QuestionNumber 这样的结构部分给了我无法解析的标识符的错误......所以我假设该结构由于某种原因没有跨文件。
此代码也是 Udemy 类(class)中的练习,我相信我的代码与老师相同,但显然这对我来说是一个问题,也许 Apple 改变了它的工作方式?
最佳答案
如果一个文件中定义的结构在另一个文件中无法工作/自动完成,请转到每个文件的文件检查器,并确保它们位于正确的“目标成员身份”下。如果同一目标成员资格下的每个文件都选中了该框,那么您应该可以开始了。如果仍然无法正常工作,请尝试取消选中并重新选中每个框,然后按命令 B。最坏的情况是,复制并粘贴所有代码并重新制作每个文件( View Controller 除外),然后重复该过程。
关于ios - 一个 Xcode 文件中的结构无法在另一个文件中工作/被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62240792/