Have a variable with multiple types in Swift

后端 未结 4 887
北海茫月
北海茫月 2021-01-12 03:24

I would like to have a variable, which can have multiple types (only ones, I defined), like:

var example: String, Int = 0
example = \"hi\"

4条回答
  •  一生所求
    2021-01-12 03:49

    You can use Tuple.

    Example:

    let example: (String, Int) = ("hi", 0)
    

    And access each data by index:

    let stringFromExampleTuple = example.0 // "hi"
    let intFromtExampleTuple = example.1 // 0
    

提交回复
热议问题