如何在问卷中提出嵌套式问题?我很困惑

时间:2019-05-02 07:11:59

标签: ios swift xcode model-view-controller

我做了多次问卷调查。总的来说,我创建了一个模型,在其中我完全规定了问题和答案,这实际上是应该的。但是,如果我不仅要一个个地问一个问题,还要根据最后一个答案问一个特定的独特问题,那么应用程序应该进行哪种处理? (可能被称为“问卷,可以选择相互交织的答案”)下一个问题或结果的出现取决于最后一个答案。如何在模型中实现它?

我想做与此scheme中相同的操作。

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 个答案:

答案 0 :(得分:1)

您可以使用几种方法:

  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,因为快速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
    }
}