1

Can't find the logic or the solution of taking the simple answer as string in a array of a ORKStepResult. It was working before with this :

                for stepResults in taskViewController.result.results! as! [ORKStepResult]  {
                for result in stepResults.results! {
                    switch result.identifier {
                    case "themaQuestionStep":
                        if let questionResult = result as? ORKQuestionResult {
                            questionResultThema  = String(questionResult.answer?.objectAtIndex(0))
                        }

After a time and update (swift & researchkit), it didn't. The line make me crazy is this one :

questionResultThema  = String(questionResult.answer?.objectAtIndex(0))

If I do this :

questionResultThema  = String(describing: questionResult.answer)

It give me the right response, but in a array :

    (
        6
    )>
Rufilix
  • 253
  • 3
  • 22

1 Answers1

1

It would be better to cast your result object to the specific question result type that you are expecting. This would give you access to the typesafe and more convenient properties of the various subclasses. For example, if you expect it to be an ORKTextQuestionResult:

if let questionResult = result as? ORKTextQuestionResult {
    questionResultThema = questionResult.textAnswer
}

Another example (to take a guess as to why questionResult.answer is an array for you). For ORKChoiceQuestionResult, you could do something like this:

if let questionResult = result as? ORKChoiceQuestionResult,
    let answer = questionResult.choiceAnswers.first as? String {
    questionResultThema = answer
}
Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75