2

我正在开发一个基于 ResearcKit 的调查应用程序,我需要根据最后五个步骤的结果的总和来确定下一步。这是我第一次使用 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)

...
4

0 回答 0