Same datatype multiple variable declaration in swift

后端 未结 2 1269
抹茶落季
抹茶落季 2021-02-01 14:33

in objective-c we can declare variable like -NSString *a,*b,*c;

in swift there a way to declare same datatype multiple variable variable rather than doing l

2条回答
  •  灰色年华
    2021-02-01 15:15

    Swift has an odd design decision here. Placing a type on a variable affects all previous non-explicitly typed variables in a multi-line definition. Same for constants.

    These two lines are equivalent (a, b and c are Double):

    var a, b, c: Double
    var a: Double, b: Double, c: Double
    

    And these two are equivalent (a and b are Int, while c and d are Double):

    var a, b: Int, c, d: Double
    var a: Int, b: Int, c: Double, d: Double
    

提交回复
热议问题