title: Converting a Swift UnsafePointer<AudioStreamBasicDescription> to a Dictionary?
body: <p>I want to know how to create a dictionary of <code>[String:AnyObject]</code> from an  <code>UnsafePointer&lt;AudioStreamBasicDescription&gt;</code></p>
  
  <p>I guess I don't understand how to work with an <code>UnsafePointer&lt;T&gt;</code> in <code>Swift</code>. Here's where I'm starting from - The <code>AVAudioFile</code> class has a <code>fileFormat</code> property which is of <code>AVAudioFormat</code> Type. <code>AVAudioFormat</code> has a <code>streamDescription</code> property which returns an <code>UnsafePointer&lt;AudioStreamBasicDescription&gt;</code> as a read-only property.</p>
  
  <p>I'd like to see what the values are in this struct and converting to a Dictionary seems like it might be reasonable goal. In fact, there already seems to be a "settings" property on the <code>AVAudioFormat</code> Class that does this but I'm interested in understanding the "right way" to access the values stored in the <code>UnsafePointer</code> myself.</p>
  
  <p>From the docs
  <code>
  Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs
  </code>
  <a href="https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html" rel="nofollow">https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html</a></p>
  
  <p>Is there a way to do an unsafe conversion after checking if the struct is not nil? Would I use an unsafeBitCast here? I'm hesitant to jump into this too much as I've read that it's "extremely dangerous"...</p>
  
  <p>I realize I can access the underlying memory with the following:</p>
  
  <pre><code>let audioFileURL:NSURL = NSBundle.mainBundle().URLForResource("FILENAME", with Extension: "mp3")
  var file:AVAudioFile?
  do {
    file = try AVAudioFile(forReading: audioFileURL)
    guard let file = file else {
      fatalError("file must not be nil")
    }
  }catch {
    print(error)
  }
  
  let format = file.processingFormat
  let asbd:AudioStreamBasicDescription = format.streamDescription.memory
  </code></pre>
  
  <p>Is that dangerous and would I need to dealloc for some reason after creating the asbd constant? </p>
  
  <p>I've tried to follow along with the post here <a href="http://sitepoint.com/using-legacy-c-apis-swift" rel="nofollow">http://sitepoint.com/using-legacy-c-apis-swift</a> but I'm just not getting it... Looking for any direction on best practices here. </p>
  
  <h2>Update:</h2>
  
  <p>Doing a bit more research, it seems that it might be possible to create the dictionary using reflect based off this post: <a href="http://freecake.angelodipaolo.org/simple-reflection-in-swift/" rel="nofollow">http://freecake.angelodipaolo.org/simple-reflection-in-swift/</a></p>
  
  <p>Here's what I have so far: </p>
  
  <pre><code>let asbdMirror = reflect(asbd)
  var asbdDict = [String: Any]()
  for i in 0..&lt;asbdMirror.count {
    let (propertyName, childMirror) = asbdMirror[i]
    asbdDict[propertyName] = childMirror.value
  } 
  </code></pre>
  
  <p>Any reason this is a bad idea?</p>