【问题标题】:ios swift ResearchKit condition for skip with NSPredicate使用 NSPredicate 跳过的 ios swift ResearchKit 条件
【发布时间】:2016-03-17 14:54:14
【问题描述】:

我正在使用 ResearchKit 开发一个小型应用程序。不幸的是,我找不到足够的例子来弄清楚如何根据上一个问题的答案跳过一个问题。 我已经把下面的代码放在一起,我知道这不好,也不能工作。 我正在寻求帮助以找出问题所在。

let textChoices2 = [

ORKTextChoice(text: "Answer 1", value: 0),

ORKTextChoice(text: "Answer 2", value: 1),

ORKTextChoice(text: "Answer 3", value: 2),

ORKTextChoice(text: "Answer 4", value: 3)
]

let questAnswerFormat2: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices2)

let questQuestionStep2 = ORKQuestionStep(identifier: "TextChoiceQuestionStep2", title: questQuestionStepTitle, answer: questAnswerFormat2)

//////////////////////////// Here I need help

let task: ORKNavigableOrderedTask = ORKNavigableOrderedTask(identifier: "xyz123", steps: steps)

//the identifier for ORKNavigableOrderedTask is a random number in my case, is it ok like this?

let resultSelector: ORKResultSelector = ORKResultSelector(stepIdentifier: "TextChoiceQuestionStep", resultIdentifier: "ImageChoiceQuestionStep”)

//stepIdentifier is the question based on which I decide if I want to skip the current question. Is it ok like this?

//resultIdentifier is the question to which I want to jump to. Is it ok like this?

let predicate: NSPredicate = ORKResultPredicate.predicateForChoiceQuestionResultWithResultSelector(resultSelector, expectedAnswerValue: "0”)

//expectedAnswerValue is the value of the answer for which I want to skip the current question. Is it ok like this?

let predicateRule: ORKPredicateStepNavigationRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers:[ (predicate, "ImageChoiceQuestionStep") ])

//“ImageChoiceQuestionStep” from the row above is the question to which I want to jump to. Is it ok like this?

task.setNavigationRule(predicateRule, forTriggerStepIdentifier: "ImageChoiceQuestionStep”)

//“ImageChoiceQuestionStep” from the row above is the question to which I want to jump to. Is it ok like this?

questQuestionStep2.task = task

【问题讨论】:

  • 由于您似乎是一个相对较新的用户,我认为值得一提的是,将合适的答案标记为已接受是一种很好的 StackOverflow 礼仪,因此问题不再显示为未回答。如果答案不令人满意,您可以对其发表评论,要求进一步澄清。
  • 谢谢@RicardoSánchez-Sáez,你是对的。我也感谢您耐心地给我答复。在过去的几天里,我一直在做其他事情。我很快就会有机会测试您向我解释的内容并将答案标记为正确。

标签: ios iphone swift nspredicate researchkit


【解决方案1】:

resultSelector (docs) 的功能是识别您希望谓词匹配的确切结果。一个resultSelector 必须有一个resultIdentifier,在question steps 中它既是question step 的标识符,也是 question result 在 (它不是您要跳转到的步骤的标识符!)。

可选地,resultSelector 可以有一个stepIdentifier(当您想要匹配表单结果时,其中几个可以包含在同一个步骤中)或taskIdentifier(如果您想匹配先前完成的任务中的结果)。我认为您的用例不需要这两个。

然后,使用有效的resultSelector 指向您感兴趣的问题,您构建一个结果谓词,并使用谓词构建一个ORKPredicateStepNavigationRule,它将具有destinationStepIdentifier(这个是您要跳转到的步骤)。

最后,将谓词步骤导航规则附加到必须触发规则的步骤(可以是谓词匹配的同一步骤,也可以是稍后出现在任务中的步骤) .

设置完所有内容并运行任务后,导航规则会在您离开它所附加到的步骤时触发,并且 destinationIdentifier 包含在导航规则指向你有条件跳转到的步骤。

请参阅TaskFactory.swift 文件(它实现了ORKTest 示例项目中的Navigable Ordered Task 项)以获取有关如何在swift 上实现条件跳转的完整示例。

还可以查看ORKNavigableOrderedTaskORKPredicateStepNavigationRuleORKResultPredicate 上的文档以获取更多信息。

我相信你可以通过使用这些来实现你想要的。


此外,还有一个正在进行的Pull Request跳过导航规则 的概念添加到ORKNavigableTask,这可能更适合您的需求。但我建议您先了解一般导航规则的工作原理。希望 PR 尽快合并。

【讨论】:

    猜你喜欢
    • 2015-09-18
    • 2015-09-22
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多