我有一个Core Data对象,它有两个属性,问题和答案。我还有一个带有标签,UITextField和按钮的视图。我想在标签中显示question属性,然后在该问题的answer属性中的UITextField类型中检查它是否正确。
所以,在viewDidLoad中,我在我的Card对象上运行了一个fetch并将结果放入一个数组中。我跟着this Stack Overflow post以了解如何遍历数组以显示标签中的每个问题属性,但现在我不确定如何检查该问题的答案属性。以下是我的源代码:
var cardFetch = [Card]()
var count = 0
@IBOutlet weak var displayLabel: UILabel!
@IBOutlet weak var inputText: UITextField!
@IBAction func nextButton(sender: AnyObject) {
displayLabel.text = cardFetch[count%cardFetch.count].question
count++
}
答案 0 :(得分:0)
尝试类似:
@IBAction func nextButton(sender: AnyObject) {
if let questionString = cardFetch[count%cardFetch.count].question as? String {
displayLabel.text = questionString
count++
}
}
@IBAction func checkAnswer(sender: AnyObject) {
if let answerString = cardFetch[count%cardFetch.count].answer as? String {
if let unwrappedTextfieldString = inputText.text as? String {
if answerString == unwrappedTextfieldString {
// Do whatever you want here
}
}
}
}
请注意使可选展开崩溃安全的展开。