Home > Back-end >  Swift ios make a generic input type with multiple type
Swift ios make a generic input type with multiple type

Time:02-15

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]
}

CodePudding user response:

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])

CodePudding user response:

struct FieldsModel<Control: UIControl> {
  let input: Control
}

let buttonModel: FieldsModel<UIButton>
let textFieldModel: FieldsModel<UITextField>
  • Related