将 Swift UnsafePointer<AudioStreamBasicDescription> 转换为字典?
Converting a Swift UnsafePointer<AudioStreamBasicDescription> to a Dictionary?
我想知道如何从 UnsafePointer<AudioStreamBasicDescription>
创建 [String:AnyObject]
的字典
我想我不明白如何在 Swift
中使用 UnsafePointer<T>
。这是我开始的地方 - AVAudioFile
class 有一个 fileFormat
属性 是 AVAudioFormat
类型。 AVAudioFormat
有一个 streamDescription
属性 其中 returns 一个 UnsafePointer<AudioStreamBasicDescription>
作为只读 属性.
我想看看这个结构中的值是什么,转换成字典似乎是一个合理的目标。事实上,AVAudioFormat
Class 上似乎已经有一个 "settings" 属性 可以执行此操作,但我有兴趣了解 "right way" 以访问值存储在 UnsafePointer
喽。
来自文档
Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs
https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html
有没有办法在检查结构是否不为 nil 后进行不安全转换?我会在这里使用 unsafeBitCast 吗?我很犹豫是否要深入了解它,因为我读到它是 "extremely dangerous"...
我意识到我可以通过以下方式访问底层内存:
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
这很危险吗?我是否需要在创建 asbd 常量后出于某种原因解除分配?
我已尝试按照 post 此处 http://sitepoint.com/using-legacy-c-apis-swift 进行操作,但我就是不明白...在这里寻找有关最佳实践的任何方向。
更新:
做更多的研究,似乎可以使用基于此 post 的反射来创建字典:http://freecake.angelodipaolo.org/simple-reflection-in-swift/
这是我目前的情况:
let asbdMirror = reflect(asbd)
var asbdDict = [String: Any]()
for i in 0..<asbdMirror.count {
let (propertyName, childMirror) = asbdMirror[i]
asbdDict[propertyName] = childMirror.value
}
为什么这是个坏主意?
您做的一切都正确,您可以使用 asbd.mSampleRate
等访问描述中的所有值。将它转换为字典是没有意义的,因为它不是它的本质,没有值的键。
除非您自己分配一个指针(使用 malloc
或 alloc
时)
,否则在使用此类指针时您也不必释放任何东西
我想知道如何从 UnsafePointer<AudioStreamBasicDescription>
[String:AnyObject]
的字典
我想我不明白如何在 Swift
中使用 UnsafePointer<T>
。这是我开始的地方 - AVAudioFile
class 有一个 fileFormat
属性 是 AVAudioFormat
类型。 AVAudioFormat
有一个 streamDescription
属性 其中 returns 一个 UnsafePointer<AudioStreamBasicDescription>
作为只读 属性.
我想看看这个结构中的值是什么,转换成字典似乎是一个合理的目标。事实上,AVAudioFormat
Class 上似乎已经有一个 "settings" 属性 可以执行此操作,但我有兴趣了解 "right way" 以访问值存储在 UnsafePointer
喽。
来自文档
Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs
https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html
有没有办法在检查结构是否不为 nil 后进行不安全转换?我会在这里使用 unsafeBitCast 吗?我很犹豫是否要深入了解它,因为我读到它是 "extremely dangerous"...
我意识到我可以通过以下方式访问底层内存:
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
这很危险吗?我是否需要在创建 asbd 常量后出于某种原因解除分配?
我已尝试按照 post 此处 http://sitepoint.com/using-legacy-c-apis-swift 进行操作,但我就是不明白...在这里寻找有关最佳实践的任何方向。
更新:
做更多的研究,似乎可以使用基于此 post 的反射来创建字典:http://freecake.angelodipaolo.org/simple-reflection-in-swift/
这是我目前的情况:
let asbdMirror = reflect(asbd)
var asbdDict = [String: Any]()
for i in 0..<asbdMirror.count {
let (propertyName, childMirror) = asbdMirror[i]
asbdDict[propertyName] = childMirror.value
}
为什么这是个坏主意?
您做的一切都正确,您可以使用 asbd.mSampleRate
等访问描述中的所有值。将它转换为字典是没有意义的,因为它不是它的本质,没有值的键。
除非您自己分配一个指针(使用 malloc
或 alloc
时)