2

I'm working on a Survey app based on ResearcKit, where I need to determine the next step based on the sum of the results of the last five steps. This is my very first time programming in swift, and it has been a while since I've done OOP, and I keep making rookie mistakes. Anyway:

In each those five steps, the user chooses one of three sentences that fits her situation the best. Each of these choices results in an answervalue (0, 1 or 2)

If the sum of the answervalues <= 5 the next step is a completionstep (no significant reason for worry), if the sum > 5 they will get a completionstep with tips on what steps to take for help.

A simplified piece of code (with only two steps to sum up) shows where my problem is - it seems like I'm doing something stupid there. Google helped me a lot with all the other predicates for my Survey, mostly with the NSCompundPredicate, but rewriting this to a situation where I could use that seems like the (very) long way around a seemingly simple problem.

Any help is greatly appreciated.

...
let choices = [
  ORKTextChoice(text: “never”, value: "0" as NSCoding & NSCopying & NSObjectProtocol), 
  ORKTextChoice(text: “sometimes”, value: "1" as NSCoding & NSCopying & NSObjectProtocol), 
  ORKTextChoice(text: “all the time”, value: "2" as NSCoding & NSCopying & NSObjectProtocol)]
 
let question1AnswerFormat : ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: choices)

let question1 = ORKQuestionStep(identifier: "question1", title: “question1”, answer: question1AnswerFormat)
question1.isOptional = false
question1.text = “Do you ever visit StackOverflow?”
steps += [question1]
    
let question2AnswerFormat : ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: choices)

let question2 = ORKQuestionStep(identifier: "question2", title: “question2”, answer: question2AnswerFormat)
question2.isOptional = false
question2.text = “Do you ever post questions on StackOverflow?”
steps += [question2]

//This part is rubbish, it doesn’t even compile, but I hope this shows what I’m looking for
let sum :Int = Int(ORKResultSelector(resultIdentifier: question1.identifier) as! String)! + Int(ORKResultSelector(resultIdentifier: question2.identifier) as! String)!
// ===

let predicate = NSPredicate(value: (sum>2)) 

let rule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [(resultPredicate: predicate, destinationStepIdentifier: risk.identifier)], defaultStepIdentifierOrNil: norisk.identifier)

task.setNavigationRule(rule, forTriggerStepIdentifier: question2.identifier)

...
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Ah well, guess I'll go for the ugly option: defining a predicate for each possible answer per question and a compoundpredicate for all 111 situations that result in a sum less than 5, with the defaultStep pointing to the sum>=5 completionstep. (there are 213 possible combinations that result in >=5, so this is the 'quick' option) Ugly, but should work. More interested in delivering a working app right now, I'll fix this later. Any thoughts on the best practice still very welcome! – Sander Termaat Feb 05 '17 at 14:40
  • 1
    I can't give you a detailed answer right now, but you're best bet is to create an `NSPredicate` with a `block` (examples [here](http://nshipster.com/nspredicate/)) and do the math for the results in the block and not try to create 111 predicates... HtH. I can try to give a more concrete answer later. – Stephen Furlani Feb 13 '17 at 17:59
  • Thanks @StephenFurlani, that looks exactly like what I need. I'll try it out tonight, and post the results here. – Sander Termaat Feb 15 '17 at 13:53

0 Answers0