skip to Main Content

I would like to defined a variable with multiple type, but i fail in this case. I want to use it as UITextfield or UIButton in other way. Please help

struct FieldsModel {
    let input: [UITextField, UIButton]
}

2

Answers


  1. You can give only 1 type to a variable . The variation in your question is not valid.You can’t assing 1..n datatype to a variable . You can use property of the structure generic . Maybe you want something like that

    struct FieldsModel<T> {
        let input: [T]
    }
    
    let structWithButton = FieldsModel<UIButton>(input: [yourButtons])
    let structWithTextField = FieldsModel<UITextField>(input: [yourTextfield])
    
    Login or Signup to reply.
  2. struct FieldsModel<Control: UIControl> {
      let input: Control
    }
    
    let buttonModel: FieldsModel<UIButton>
    let textFieldModel: FieldsModel<UITextField>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search