[swift-users] Array elements assign to variables

TUNG CK cktung at mac.com
Fri Jan 27 22:21:29 CST 2017


Thanks 
I think we could only do this in swift

var (r,g,b,brightness) = (pixel[0],pixel[1],pixel[2],pixel[3])

> Alex Blewitt <alblue at apple.com> 
> 
>> On 27 Jan 2017, at 11:27 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/20170128/1fc5a0ec/attachment.html>


More information about the swift-users mailing list