Search code examples
iosswiftresearchkit

ResearchKit stepResult for a TextChoiceQuestion


I'm using ResearchKit's stepResultForStepIdentifier method successfully for other question types, but can't find correct syntax to pre-populate the results for a TextChoiceQuestion.

Below is an unsuccessful attempt at setting the result for the sample TextChoice question in ORKCatalog. Any advice on correct approach?

func stepResultForStepIdentifier(stepIdentifier: String) -> ORKStepResult? {

    var stepResults = [ORKQuestionResult]()

    if stepIdentifier == "TextChoiceQuestionStep" {

        var questionDefault = ORKChoiceQuestionResult(identifier: stepIdentifier)

        questionDefault.choiceAnswers?.append("choice_2")

        stepResults.append(questionDefault)

    }

    var defaults = ORKStepResult(stepIdentifier: stepIdentifier, results: stepResults)

    return defaults
}

Solution

  • Is the choiceAnswers array nil? When you do questionDefault.choiceAnswers?.append, choiceAnswers might be nil, so this might do nothing.

    Instead do questionDefault.choiceAnswers = ["choice_2"]