0

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
}
Karlo A. López
  • 2,548
  • 3
  • 29
  • 56
Brendenw
  • 785
  • 1
  • 6
  • 22

1 Answers1

1

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"]

jwe
  • 436
  • 2
  • 4
  • Thanks. That works. I added a check for whether the array is nil - `if questionDefault.choiceAnswers == nil { questionDefault.choiceAnswers = ["choice_2"] } else { questionDefault.choiceAnswers?.append("choice_2") }` – Brendenw Jun 05 '15 at 16:42