variable size array

Hello,


How can I access variable size arrays in Swift 1.2?

For example


var layout:AudioChannelLayout = ...


for var i:UInt32 = 0; i < layout.mNumberChannelDescriptions; i++

{

if layout.mChannelDescriptions[i].mChannelLabel == kAudioChannelLabel_Left { /*some action*/ } // 'AudioChannelDescription' does not have a member named 'subscript'

}

Type of layout.mChannelDescriptions is (AudioChannelDescription)


TIA,


Jan E.


BTW it would be nice if the Analyzer's remarks could be copied

Replies

That worked!

Neat-o!

One thing to note here is that an

UnsafeMutableBufferPointer
is actually a
Collection
, so you can use
Collection
methods on it. For example, you can use
map(_:)
to map from the elements to a
String
and then
joined(separator:)
to join those strings together, like so:
channelLayoutDescriptionString = withUnsafeMutablePointer(to: &channelLayout.mChannelDescriptions) { start in
    let channelDescriptions = UnsafeMutableBufferPointer(start: start, count: Int(channelLayout.mNumberChannelDescriptions))
    return channelDescriptions.map { desc in
        desc.mChannelLabel.audioChannelLabelString
    }.joined(separator: ", ")
}

Much nicer (IMO, obviously) than messing around with a bunch of mutable state (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi there! I'm back with a related question.


Say I want to create an AudioChannelLayout in Swift from scratch - how would I go about creating a variable sized array in Swift that I can assign to mChannelDescriptions?


For example, say I have a Swift Array of AudioChannelDescriptions and I want to make that a C tuple variable sized array to set the the AudioChannelLayout's mChannelDescriptions with.