ios - Generics for repeated task with associated data - Swift -


i've been trying wrap mind around seemingly simple task, keep getting nowhere.

my goal create user-choice flow. let's have list of food-related questions :

what favourite breakfast? have dinner? how      meat cooked? whats favourite spaghetti sauce? e.t.c. 

and set of reply options each question

q1: <<pancakes|waffles>>, q2: <<steak|spaghetti>>, q3: <<raw|welldone>>, q4: <<bolognese|simple cheese>> 

how load next question set of reply options depending on users choice in previous question? main trouble how make generic , data-driven - without need bunch of conditionals.

i've been trying work arrays, nsdictionaries, nsregularexpressions can't come proper logical solution.

any insights appreciated!

thank in advance.

an alternative dictionaries custom class. think improves readability may have own opinion.

class question {     var ask: string     var answers: [string]      var nextquestions = [question?]()      init(question: string, ans: [string]) {         self.ask = question         self.answers = ans     }      func nextquestion(answer: string) -> question? {         var result: question? = nil         if let index = find(self.answers, answer) {             result = self.nextquestions[index]         }         return result     } }  // set test data let q1 = question(question: "what favourite breakfast", ans: ["pancakes", "waffles"]) let q2 = question(question: "what have dinner", ans: ["steak", "spaghetti"]) let q3 = question(question: "how meat cooked", ans: ["raw", "welldone"]) let q4 = question(question: "what's favourite spaghetti sauce", ans: ["bolognese", "simple cheese"])  // quick , dirty. // better have func hide implementation. q1.nextquestions.append(q2) q1.nextquestions.append(q2) q2.nextquestions.append(q3) q2.nextquestions.append(q4)  // pretend "spaghetti" answer q2 var thequestion = q2 let useranswer = "spaghetti"  if let = thequestion.nextquestion(useranswer) {     thequestion = } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -