我正在开发一个基于ResearcKit的Survey应用程序,我需要根据最后五个步骤的结果总和来确定下一步。这是我第一次使用swift进行编程,自从我完成OOP以来已经有一段时间了,而且我一直在犯新秀错误。无论如何:
在这五个步骤中,用户选择最适合她情况的三个句子中的一个。这些选择中的每一个都会产生答案值(0,1或2)
如果答案值的总和< = 5,则下一步是完成步骤(无需担心的重要原因),如果总和> 5他们将获得一个完整的步骤,提供有关采取哪些步骤寻求帮助的提示。
一段简化的代码(只有两个步骤可以总结)显示我的问题所在 - 似乎我在那里做了一些愚蠢的事情。 Google为我的调查中的所有其他谓词提供了很多帮助,主要是使用NSCompundPredicate,但是将其重写为我可以使用它的情况似乎是(非常)长期解决一个看似简单的问题。
非常感谢任何帮助。
...
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)
...