[swift-users] Array elements assign to variables

Alex Blewitt alblue at apple.com
Fri Jan 27 05:40:29 CST 2017


On 27 Jan 2017, at 11:27, TUNG CK via swift-users <swift-users at swift.org> wrote:
> 
> Python, we have this
>    for pixel in pixels:
>        r, g, b, brightness = pixel
>        .....
> 
> I wonder how to do it in swift? is there a more swifty way to do something like this below?
>    for pixel in pixels {
>        var r = pixel[0]
>        var g = pixel[1]
>        var b = pixel[2]
>        var brightness = pixel[3]
>         .....
>     }

If Pixel is a tuple instead of an array, you can do:

var (r,g,b,brightness) = pixel

Alex

  1> let Pixel = (1,2,3,4)
Pixel: (Int, Int, Int, Int) = {
  0 = 1
  1 = 2
  2 = 3
  3 = 4
}
  2> var (r,g,b,brightness) = Pixel
r: Int = 1
g: Int = 2
b: Int = 3
brightness: Int = 4
  3>  

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170127/53d87fdc/attachment.html>


More information about the swift-users mailing list