ios - Swift: Create array with different custom types -


i'm making registration form app. lets users create questions , display created questions in uitableview. users can create 2 types of questions, text input(using textfields) , multiple choice(using segmentedcontrol). here class created make happen.

class question { var label: string var required: int  // create question init (label: string, required: int) {     self.label = label     self.required = required      if (self.required == 0) {         self.label = self.label + " *"     } } }  class textinput: question { var placeholder: string  init (placeholder: string, label: string, required: int) {     self.placeholder = placeholder     super.init(label: label, required: required) } }  class multichoice: question { var answers: [string]  init(answers: [string], label: string, required: int) {     self.answers = answers     super.init(label: label, required: required) } } 

i need populate array textinput or multichoice types displayed in uitableview. in c++ can use templates accomplish arbitrary type issue. there way in swift, , if pointers on how these classes?

first off. change question class follows:

var required: bool  init (label: string, required: bool) {     self.label = label     self.required = required      if self.required {         self.label = self.label + " *"     } } 

next, can use array of type [question] achieve you're looking for. can specific actions based on class using guard or if let structures.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -