[swift-users] Array elements assign to variables

Ray Fix rayfix at gmail.com
Fri Jan 27 22:48:00 CST 2017


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 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]
>         .....
>     }
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

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


More information about the swift-users mailing list