将Swift UnsafePointer <audiostreambasicdescription>转换为字典?

时间:2015-07-15 19:59:52

标签: ios swift avfoundation swift2

我想知道如何从String M2 = JOptionPane.showInputDialog(Name2 + "'s Turn. Here are your options\n 1.Invade \n 2.Buy \n 3.End Turn \n 4.Check Money Balance \n 5.Check Soldier Count \n 6.Citizen's Hapinness \n 7.Owned Islands \n 8.Check Rules", "Type the Number of the action you want to take place"); if (M2.equals("1")) { String Inv=JOptionPane.showInputDialog(null, "Open up the map and Check the island that you are in! If you dont remember the islands name type B to go back and then go into Owned Islands and come back! Then see the attack option you have and choose where you want to attack.Type A to Attack"); if(Inv.equalsIgnoreCase("A")){ Attack=false; Attack=true; continue attack; }else{ continue P2Menu2; } }

创建[String:AnyObject]字典

我想我不明白如何使用UnsafePointer<AudioStreamBasicDescription>中的UnsafePointer<T>。这是我开始的地方 - Swift类的AVAudioFile属性属于fileFormat类型。 AVAudioFormat具有AVAudioFormat属性,该属性返回streamDescription作为只读属性。

我想看看这个结构中的值是什么,转换为字典似乎可能是合理的目标。事实上,似乎已经有一个&#34;设置&#34; UnsafePointer<AudioStreamBasicDescription>班级的财产,但我有兴趣了解正确的方式&#34;访问AVAudioFormat我自己存储的值。

来自文档 UnsafePointer https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html

在检查结构是否为零之后,有没有办法进行不安全的转换?我会在这里使用unsafeBitCast吗?我对此过于犹豫不已,因为我已经读到它非常危险&#34; ...

我意识到我可以使用以下内容访问底层内存:


Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs

这是危险的,在创建asbd常量后,我是否需要因某种原因而dealloc?

我已经尝试跟随这里的帖子http://sitepoint.com/using-legacy-c-apis-swift,但我还没有得到它......在这里寻找关于最佳做法的任何方向。

更新

进行更多研究,似乎可以使用基于此帖子的反射来创建字典:http://freecake.angelodipaolo.org/simple-reflection-in-swift/

这是我到目前为止所拥有的:

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

这是个坏主意吗?

1 个答案:

答案 0 :(得分:1)

您正在正确地执行所有操作,您可以使用asbd.mSampleRate等访问说明中的所有值。将它转换为字典是没有意义的,因为它不是它的原因,没有值的键。

除非您自己分配一个指针(使用mallocalloc时)

,否则在使用此类指针时也无需取消分配任何内容。