title: How to cast UnsafeMutableRawPointer! to UnsafeMutablePointer<Float> in Swift 3.0 or newer?
body: <p>I am working with Accelerate library for my iOS app. I built this app in Swift 2.0 a couple years ago and it was working fine. Now that Swift is updated to 3.0 and 4.0, I have to convert most of the code to the current syntax. I have run the converter but I still have some syntax errors. This is the one that I am having trouble the most fixing. I couldn't seem to find an equivalently succinct way to fix it.</p>
  
  <p>I made functions for basic arithmetic operation on images. 
  This is one of them:</p>
  
  <pre><code>public func add(_ left: Image, right: Image) -&gt; Image! {
      let results = Image.init(rows: left.size.rows, columns: left.size.columns, pixelSize: left.pixelSize)
      let leftOperand = UnsafeMutablePointer&lt;Float&gt;(left.buffer.data)
      let rightOperand = UnsafeMutablePointer&lt;Float&gt;(right.buffer.data)
      let destination = UnsafeMutablePointer&lt;Float&gt;(results.buffer.data)
      vDSP_vadd(leftOperand, 1, rightOperand, 1, destination, 1, UInt(left.lengthOfBuffer))
      return results
  }
  </code></pre>
  
  <p>The class <code>Image</code> has a field <code>buffer</code> which is a <code>vImage_Buffer</code>and is defined like such,</p>
  
  <pre><code>open class Image {
  
  open var buffer: vImage_Buffer
  
  ....}
  </code></pre>
  
  <p><strong><em>The problem:</em></strong>
  Calling <code>buffer.data</code> returns a type of <code>UnsafeMutableRawPointer!</code>. The <code>vDSP_add</code> function requires <code>UnsafeMutablePointer&lt;Float&gt;</code> for its parameters. I used to be able to cast it to an <code>UnsafeMutablePointer&lt;Float&gt;</code> object right away. Looks like the initializer is deprecated now. Thus, on the codes like <code>let leftOperand = UnsafeMutablePointer&lt;Float&gt;(left.buffer.data)</code>, I get this error</p>
  
  <pre><code>Cannot invoke initializer for type 'UnsafeMutablePointer&lt;Float&gt;' with an argument list of type '(UnsafeMutableRawPointer!)'.
  </code></pre>
  
  <p>I don't know if I overlook something very simple here, yet I still haven't fixed this. But, I am looking for a solution that doesn't require manually copying each element using a loop, since this function is called pretty frequently in the app. </p>
  
  <p>Just some documentation links that might help:</p>
  
  <ul>
  <li><a href="https://developer.apple.com/documentation/accelerate/vimage_buffer" rel="noreferrer">https://developer.apple.com/documentation/accelerate/vimage_buffer</a></li>
  <li><a href="https://developer.apple.com/documentation/accelerate/1450275-vdsp_vsadd" rel="noreferrer">https://developer.apple.com/documentation/accelerate/1450275-vdsp_vsadd</a></li>
  <li><a href="https://developer.apple.com/documentation/swift/unsafemutablepointer#topics" rel="noreferrer">https://developer.apple.com/documentation/swift/unsafemutablepointer#topics</a></li>
  </ul>
  
  <p>Thank you in advance!</p>