title: Swift - dynamically initialise ResearchKit classes based on a server response
body: <p>ResearchKit has <code>ORKOrderedTask</code> a class to add steps like Instruction, Survey, Activity to it. Through class mapping I determine the given response from server comes under which step. And my mapping is as follows  </p>
  
  <pre><code>let classMapping = [
  "ORKInstructionStep": ORKInstructionStep.self,
  "ORKQuestionStep": ORKQuestionStep.self,
  "ORKFormStep": ORKFormStep.self,
  "ORKFormItem": ORKFormItem.self
  ]
  </code></pre>
  
  <p>If I add a question step to <code>ORKOrderedTask</code>. And the question step structure to store data from server is as follows </p>
  
  <pre><code>struct QuestionGroup {
  let identifier, title, text : String
  let answerFormat: ORKAnswerFormat
  init(identifier: String, title: String, text: String, answerFormat: ORKAnswerFormat) {
      self.identifier   = identifier
      self.title = title
      self.text  = text
      self.answerFormat = ORKAnswerFormat()
  }
  }
  </code></pre>
  
  <p>And I store the date to the question group structure from server as follows </p>
  
  <pre><code>LIbraryAPI.sharedInstance.questionList.append(QuestionGroup(identifier: row["objectId"]!.string!, 
  title: row["title"]!.string!, text: row["text"]!.string!, answerFormat: ORKBooleanAnswerFormat()))
  </code></pre>
  
  <p>In the above code the <strong>answerFormat</strong> is hard coded as of now, but can be of any of the answer format in this following enum </p>
  
  <pre><code>enum AnswerFormat: Int {
  case ORKScaleAnswerFormat = 0,
  ORKContinuousScaleAnswerFormat,
  ORKValuePickerAnswerFormat,
  ORKImageChoiceAnswerFormat,
  ORKTextChoiceAnswerFormat,
  `ORKBooleanAnswerFormat`,
  ORKNumericAnswerFormat,
  ORKTimeOfDayAnswerFormat,
  ORKDateAnswerFormat,
  ORKTextAnswerFormat,
  ORKTimeIntervalAnswerFormat
     }
  </code></pre>
  
  <p>For each answer format there will be different argument to pass. Following is the final code where I add the question step to <strong>ORKOrderedTask</strong>.</p>
  
  <pre><code>  if let questionStep  = classMapping["ORKQuestionStep"] as? ORKQuestionStep {
      let tempStep = questionStep.dynamicType
      let group = LIbraryAPI.sharedInstance.questionList
      for eachGroup in group {
          let finalQuestionStep = tempStep(identifier: eachGroup.identifier, title: eachGroup.title, answer: eachGroup.answerFormat)
          steps += [finalQuestionStep]
      }
  }
  </code></pre>
  
  <p>So my problem is the server passes me different kind of answer format for each question.  How do I dynamically create the instance of each answer format that comes under <code>AnswerFormat</code> enum? Because <code>ORKBooleanAnswerFormat</code> does not have any arguments, on the other hand <code>ORKTextChoiceAnswerFormat</code> can have an array of strings as an argument and Date answer format must be supplied with max and min date so on and so forth. </p>
  
  <p>So how do I manage all these and bring under one single class or function to make it reusable for all the answer format classes? </p>