【问题标题】:How do I access this array of structs - Swift如何访问这个结构数组 - Swift
【发布时间】:2015-06-05 11:58:43
【问题描述】:

我创建了一个名为“questions”的数组,其中充满了结构“QuizQuestion”的实例 - 基本上我希望“nextQuestion”函数从结构(问题)数组中提取下一个问题并将其显示到用户(我知道如何通过标签等呈现数据,我只是不知道如何访问它)。

我尝试调用数组索引,然后调用实例,但这似乎不起作用。我还尝试创建一个“QuizQuestion”实例作为变量,并在“NextQuestion”函数中使用它,但我不知道如何自动从“问题”中提取问题。

谢谢

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var questionLabel: UILabel!


@IBOutlet weak var answerLabel: UILabel!

@IBOutlet weak var explanationLabel: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    questionVariable()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

struct QuizQuestion {
    let question: String
    let answer: Bool
    let explanation: String

}

let questions: [QuizQuestion] = [

    QuizQuestion(question: "Red is a four letter word", answer: false, explanation: "Red is not a four letter word"),
    QuizQuestion(question: "Dog is a three letter word", answer: true, explanation: "Dog is most likely a three letter word"),
    QuizQuestion(question: "Cow is a three letter word", answer: true, explanation: "Cow is most likely a three letter word")

]

var nextQuestion = QuizQuestion.self[1]



func questionVariable() {

    if nextQuestion.answer == true {
        questionLabel.text = nextQuestion.question
        explanationLabel.text = nextQuestion.explanation
        answerLabel.text = "Correct"

    }
}
}

【问题讨论】:

  • 您的意思是输入var nextQuestion = questions[0]吗?
  • 哦,是的,抱歉-我一定没有在测试时更改它!谢谢

标签: arrays swift struct instance


【解决方案1】:

您可以定义一个嵌套函数(Swift 中最简单的闭包形式),如下所示:

func getNextQuestion() -> (Int) -> QuizQuestion!
{
    func incrementor(var index:Int) -> QuizQuestion! {
        if index < questions.count && index >= 0 {
            return questions[index]
        }
        else {
            return nil
        }
    }
    return incrementor
}

然后您可以访问您的问题列表,如下所示

let nextQuestion = getNextQuestion()
var question_1 = nextQuestion(0)
var question_2 = nextQuestion(1)
var question_3 = nextQuestion(2)

println("1.question text: \(question_1.question)")
println("2.question text: \(question_2.question)")
println("3.question text: \(question_3.question)")

【讨论】:

    【解决方案2】:

    从数组中检索问题的简单函数。声明一个实例变量来保存当前索引。

    var currentIndex = 0
    
    func nextQuestion() -> QuizQuestion {  
    // initialize current question
    var currentQuestion: QuizQuestion = QuizQuestion(question: "", answer: false, explanation: "")
    
    if currentIndex < questions.count {
        currentQuestion =  questions[currentIndex]
    }
    currentIndex++
    
    return currentQuestion
    }
    

    【讨论】:

    • 非常感谢您的帮助!
    • 你好,我已经在我的代码中实现了这个功能,它工作得很好。但是,我在创建“上一个问题”按钮的逻辑上苦苦挣扎。我试过让按钮减去 currentIndex 1 并调用 nextQuestion 函数,但它似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2014-09-11
    • 2021-02-11
    • 2018-06-08
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多