0

So I am trying to write a code for ResearchKit on 7.1.1 with Swift and I am having trouble with asking multiple question types. By question type I mean asking the user to input "text field" data and "selecting one out of a list" data.
It works if I run the code with just question 1 and 2. But if I include question 3, the code breaks down and gives an error.

import Foundation
import ResearchKit

public var SurveyTask: ORKOrderedTask {

var steps = [ORKStep]()

//TODO: add instructions step
let instructionStep = ORKInstructionStep(identifier: "IntroStep")
instructionStep.title = "Introduction"
instructionStep.text = "10 Questions that you can just answer or skip! (Althought we do hope you will answer them.)"
steps += [instructionStep]

//TODO: add questions

//question 1
let nameAnswerFormat = ORKTextAnswerFormat(maximumLength: 2)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle = "What is the child's age?"
let nameQuestionStep = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle, answer: nameAnswerFormat)
steps += [nameQuestionStep]

//question 2
let questQuestionStepTitle = "Has there been any other incidences of this disease in other family members?"
let textChoices = [
    ORKTextChoice(text: "Yes", value: 0),
    ORKTextChoice(text: "No", value: 1),
]
let questAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices)
let questQuestionStep = ORKQuestionStep(identifier: "TextChoiceQuestionStep", title: questQuestionStepTitle, answer: questAnswerFormat)
steps += [questQuestionStep]

  //question 3
let questQuestionStepTitle2 = "Is the tumor in one or both eyes?"
let textChoices2 = [
    ORKTextChoice(text: "Right Eye", value: 0),
    ORKTextChoice(text: "Left Eye", value: 1),
    ORKTextChoice(text: "Both Eyes", value: 2),
]
let questAnswerFormat2: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices2)
let questQuestionStep2 = ORKQuestionStep(identifier: "TextChoiceQuestionStep", title: questQuestionStepTitle2, answer: questAnswerFormat2)
steps += [questQuestionStep2]

/*//question 4
let questQuestionStepTitle3 = "Do you know anything about genetic counselling?"
let textChoices3 = [
    ORKTextChoice(text: "Yes", value: 0),
    ORKTextChoice(text: "No", value: 1),
]
let questAnswerFormat3: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices3)
let questQuestionStep3 = ORKQuestionStep(identifier: "TextChoiceQuestionStep", title: questQuestionStepTitle3, answer: questAnswerFormat3)
steps += [questQuestionStep3]

//question 5
let nameAnswerFormat2 = ORKTextAnswerFormat(maximumLength: 300)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle2 = "How did you first realize your child was sick?"
let nameQuestionStep2 = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle2, answer: nameAnswerFormat2)
steps += [nameQuestionStep2]

//question 6
let nameAnswerFormat3 = ORKTextAnswerFormat(maximumLength: 300)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle3 = "What treatment is the child taking?"
let nameQuestionStep3 = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle3, answer: nameAnswerFormat3)
steps += [nameQuestionStep3]

//question 7
let nameAnswerFormat4 = ORKTextAnswerFormat(maximumLength: 100)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle4 = "How long has the treatment been ongoing?"
let nameQuestionStep4 = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle4, answer: nameAnswerFormat4)
steps += [nameQuestionStep4]

//question 8
let nameAnswerFormat5 = ORKTextAnswerFormat(maximumLength: 300)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle5 = "What major questions do you have about the process and about the treatment itself?"
let nameQuestionStep5 = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle5, answer: nameAnswerFormat5)
steps += [nameQuestionStep5]

//question 9
let nameAnswerFormat6 = ORKTextAnswerFormat(maximumLength: 300)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle6 = "Are there any side effects of the treatment?? State any if there is."
let nameQuestionStep6 = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle6, answer: nameAnswerFormat6)
steps += [nameQuestionStep6]

//question 10
let nameAnswerFormat7 = ORKTextAnswerFormat(maximumLength: 100)
nameAnswerFormat.multipleLines = false
let nameQuestionStepTitle7 = "How has your day been? "
let nameQuestionStep7 = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle7, answer: nameAnswerFormat7)
steps += [nameQuestionStep7]*/




//TODO: add summary step
let summaryStep = ORKCompletionStep(identifier: "SummaryStep")
summaryStep.title = "Right. Off you go!"
summaryStep.text = "That was easy!"
steps += [summaryStep]

return ORKOrderedTask(identifier: "SurveyTask", steps: steps)
}

Any help would be appreciated, thanks!

ketan
  • 19,129
  • 42
  • 60
  • 98
Tendouji
  • 11
  • 3

1 Answers1

0

Question 3:
Try changing identifier to #2:

let questQuestionStep2 = ORKQuestionStep(identifier: "TextChoiceQuestionStep2",...
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
CPH
  • 1