如何在问卷中制作嵌套问题?我很困惑



我做了多个问卷。通常,我创建了一个模型,其中我完全规定了问题和答案,因为它实际上应该是。但是,如果我不仅要一个一个问题,还要根据最后一个答案提出一个特定的独特问题,应该在应用程序中进行哪种处理?(可能这称为"调查表具有相互交织答案的选择"(下一个问题或结果的外观将取决于最后一个答案。如何在模型中实现它?

我想做与此方案中的相同。

struct PossibleAnswer {
var text: String
var type: TypeOfLanguage
var nextQuestion: Question? 

}

enum TypeOfLanguage: String {
case python = "Python"
case java = "Java"
case c = "C"
case cPlusPlus = "C++"
case javaScript = "JavaScript"
case cSharp = "C#"
case ruby = "Ruby"
case php = "PHP"
case swift = "Swift"
case next
var definition: String {
    switch self {
    case .python:
        return "Some text"
    case .java:
        return "Some text"
    case .c:
        return "Some text"
    case .cPlusPlus:
        return "Some text"
    case .javaScript:
        return "Some text"
    case .cSharp:
        return "Some text"
    case .ruby:
        return "Some text"
    case .php:
        return "Some text"
    case .swift:
        return "Some text"
    case .next:
        return ""
    }
}

}

struct Question {
var text: String
var answers: [PossibleAnswer]
static func loadData() -> [Question] {
    return [Question(text: "Why do you want to learn programming?", answers: [
        PossibleAnswer(text: "For my kids", type: .python, nextQuestion: nil),
        PossibleAnswer(text: "Make money", type: .next, nextQuestion:
            Question(text: "Make money", answers: [
            PossibleAnswer(text: "Get a job", type: .next, nextQuestion:
                Question(text: "Which platform/field?", answers: [
                    PossibleAnswer(text: "I want to work for big tech companies", type: .next, nextQuestion:
                        Question(text: "Which one?", answers: [
                            PossibleAnswer(text: "Facebook", type: .python, nextQuestion: nil),
                            PossibleAnswer(text: "Google", type: .python, nextQuestion: nil),
                            PossibleAnswer(text: "Microsoft", type: .cSharp, nextQuestion: nil),
                            PossibleAnswer(text: "Apple", type: .swift, nextQuestion: nil)])),
                    PossibleAnswer(text: "Doesn't matter, i just want money!", type: .java, nextQuestion: nil)]))]))])]}}

您可以使用几种方法:

  1. 您可以定义问答的ID,并参考使用它。这样:
struct Identifier<T>: Hashable {
    let value: String
    func hash(into hasher: inout Hasher) {
        hasher.combine(value)
    }
}
struct Answer {
    let id: Identifier<Answer>
    let text: String
    var nextQuestion: Identifier<Question>?
}
struct Question {
    let id: Identifier<Question>
    let text: String
    var answers: [Answer] = []
}
var allQuestions: [Identifier<Question>: Question] = [:]
let answer = Answer(id: Identifier(value: "answer id"), text: "Answer text", nextQuestion: Identifier(value: "Q 2"))
let deadendAnswer = Answer(id: Identifier(value: "deadendAnswer id"), text: "Deadend Answer text", nextQuestion: nil)
let question1 = Question(id: Identifier(value: "Q 1"), text: "Question 1", answers: [answer, deadendAnswer])
let question2 = Question(id: Identifier(value: "Q 2"), text: "Question 2", answers: [])
allQuestions[question1.id] = question1
allQuestions[question2.id] = question2
func nextQuestion(for answer: Answer) -> Question? {
    guard let id = answer.nextQuestion else {
        return nil
    }
    return allQuestions[id]
}
  1. 您可以将struct切换到class,并且由于Swift struct是一个值类型,不能用于递归结构。但是 class是一种参考类型,递归可以正常工作。

  2. 另外,您仍然可以使用struct,但将nextQuestion存储为参考:

struct Question {
    let text: String
    var answers: [Answer] = []
}
struct Answer {
    let text: String
    var nextQuestion: Container<Question>?
}
class Container<T> {
    let value: T
    init(_ value: T) {
        self.value = value
    }
}

最新更新