Multiple variable assignment in Swift

前端 未结 2 1970
遇见更好的自我
遇见更好的自我 2020-11-30 06:18

How do I assign multiple variables in one line using Swift?

    var blah = 0
    var blah2 = 2

    blah = blah2 = 3  // Doesn\'t work???
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 07:00

    As the accepted answer says: You can get some tighter syntax, however you cannot assign from a to b to c without using multiple lines (for safety, probably). Here's an example of some more concise syntax to declare and assign multiple variables in one line:

    var red, green, blue, alpha : CGFloat
    (red, green, blue, alpha) = (0.0, 0.0, 0.0, 0.0)
    ledColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
    

提交回复
热议问题