title: Swift get value from UnsafeMutablePointer<Void> using UnsafePointer<String>
body: <p>I am trying to pass <code>contextInfo</code> of type<code>UnsafeMutablePointer&lt;Void&gt;</code> to <code>UISaveVideoAtPathToSavedPhotosAlbum</code> and use it in the callback function.  For some reason I am unable to access <code>contextInfo</code> as a string using <code>UnsafePointer&lt;String&gt;(x).memory</code> when I am in the callback function.</p>
  
  <p>I am pretty sure it is something simple I am missing but have spent way to many hours trying to figure this out.</p>
  
  <p>Below is some code that I have tried.</p>
  
  <p>The following code works.</p>
  
  <pre><code>var testStr:String = "hello"
  takesAMutableVoidPointer(&amp;testStr)
  
  func takesAMutableVoidPointer(x: UnsafeMutablePointer&lt;Void&gt;){
      var pStr:String = UnsafePointer&lt;String&gt;(x).memory
      println("x = \(x)")
      println("pStr = \(pStr)")
  }
  </code></pre>
  
  <p>However the following code does not work.</p>
  
  <pre><code>var testStr:String = "hello"
  
  if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath){ //the filePath is compatible
      println("Compatible")
      //UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, nil, nil)
      UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, "video:didFinishSavingWithError:contextInfo:", &amp;testStr)
  }
  else{
      println("Not Compatible")
  }
  
  func video(video: NSString, didFinishSavingWithError error:NSError, contextInfo:UnsafeMutablePointer&lt;Void&gt;){
      var pStr:String = UnsafePointer&lt;String&gt;(contextInfo).memory
      println("contextInfo = \(contextInfo)")
      println("pStr = \(pStr)")
  }
  </code></pre>
  
  <p>Once I get to the following line:</p>
  
  <pre><code>var pStr:String = UnsafePointer&lt;String&gt;(contextInfo).memory
  </code></pre>
  
  <p>I keep getting the following error:</p>
  
  <pre><code>Thread 1: EXC_BAD_ACCESS(code=1, address=0x0)
  </code></pre>
  
  <p>Any help with this would be greatly appreciated.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Rintaro commented that testStr needs to be top level but the following code works.</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          var testStr:String = "hello"
          takesAMutableVoidPointer(&amp;testStr)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      func takesAMutableVoidPointer(x: UnsafeMutablePointer&lt;Void&gt;){
          var answer = UnsafePointer&lt;String&gt;(x).memory
          println("x = \(x)")
          println("answer = \(answer)")
      }
  }
  </code></pre>
  
  <p>I am trying not to use global variables unless I have to.  I may have to but since I am able to execute the above code, it seems as though I do not need to use a global variable.</p>