1

The following code:

  var mutableDataP = UnsafeMutablePointer<Int16>(audioBuffer.mData)
  let stereoSampleArray = UnsafeMutableBufferPointer(
      start: mutableDataP,
      count: nBytesInBuffer/sizeof(Int16)    // Int16 audio samples
  )

gives the following error:

Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer<_>'

What is an UnsafeMutablePointer<__> and how do I cast to it? I tried all the casting variations I could think of and got un-understandable diagnostics for each and I've run out of ideas. I find the documentation on the various UnsafeMutablePointer types unhelpful, and no mention at all of '<_>'.

RobertL
  • 14,214
  • 11
  • 38
  • 44
  • The double underscore in UnsafeMutablePointer<__> is actually a single underscore. I entered double because a single underscore vanishes in StackExchange. – RobertL Sep 20 '16 at 17:13
  • This might help: http://stackoverflow.com/questions/25359735/casting-between-different-unsafepointert-in-swift. – Martin R Sep 20 '16 at 17:51

1 Answers1

0

Try this change:

let stereoSampleArray = withUnsafeMutablePointer(to: &audioBuffer.mData){
       return UnsafeMutableBufferPointer(
              start: $0,
              count: nBytesInBuffer/MemoryLayout<Int16>.size    // Int16 audio samples
             )
       }

Where audioBuffer.mData is a var

Jans
  • 11,064
  • 3
  • 37
  • 45
  • Putting '&' in front of audioBuffer gives "ambiguous use of init". And 'MemoryLayout' is an unresolved identifier. – RobertL Sep 20 '16 at 17:58
  • Probably the 2nd problem (unresolved identifier) is because I didn't convert the project to Swift 3 yet. I'm trying to get it to build successfully so I can use the convert tool. So I just need to find out how to write the expression for mutableDataP. – RobertL Sep 20 '16 at 18:19
  • I see, check the edited answer. replace `MemoryLayout` if you're still in swift2. – Jans Sep 20 '16 at 19:34