Array elements assign to variables


(CK TUNG) #1

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]
         .....
     }


(Alex Blewitt) #2

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>

···

On 27 Jan 2017, at 11:27, TUNG CK via swift-users <swift-users@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]
        .....
    }


(Ray Fix) #3

How about:

typealias PixelTuple = (red: UInt8, green: UInt8, blue: UInt8, brightness: UInt8)

let pixelCount = 10

let pointer = UnsafeMutableRawPointer.allocate(bytes: 4*pixelCount, alignedTo: 16)
defer {
  pointer.deallocate(bytes: 4*pixelCount, alignedTo: 16)
}

let tuplesPointer = pointer.bindMemory(to: PixelTuple.self, capacity: pixelCount)
let tuples = UnsafeMutableBufferPointer(start: tuplesPointer, count: pixelCount)

for tuple in tuples {
  print(tuple.red, tuple.green, tuple.blue, tuple.brightness)
}

···

On Jan 27, 2017, at 3:27 AM, TUNG CK via swift-users <swift-users@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]
        .....
    }
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(CK TUNG) #4

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@apple.com>

On 27 Jan 2017, at 11:27 via swift-users <swift-users@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>