转换Swift UnsafePointer< AudioStreamBasicDescription>到字典? [英] Converting a Swift UnsafePointer<AudioStreamBasicDescription> to a Dictionary?
问题描述
我想知道如何从UnsafePointer<AudioStreamBasicDescription>
我想我不明白如何使用Swift
中的UnsafePointer<T>
.这就是我的出发地-AVAudioFile
类具有fileFormat
类型的fileFormat
属性. AVAudioFormat
具有streamDescription
属性,该属性返回UnsafePointer<AudioStreamBasicDescription>
作为只读属性.
I guess I don't understand how to work with an UnsafePointer<T>
in Swift
. Here's where I'm starting from - The AVAudioFile
class has a fileFormat
property which is of AVAudioFormat
Type. AVAudioFormat
has a streamDescription
property which returns an UnsafePointer<AudioStreamBasicDescription>
as a read-only property.
我想看看此结构中的值是什么,并将其转换为Dictionary似乎是一个合理的目标.实际上,在AVAudioFormat
类上似乎已经有一个设置"属性可以做到这一点,但是我有兴趣了解正确的方式"来访问自己存储在UnsafePointer
中的值.
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 AVAudioFormat
Class that does this but I'm interested in understanding the "right way" to access the values stored in the UnsafePointer
myself.
来自文档
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
From the docs
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
在检查struct是否为nil之后,有没有办法进行不安全的转换?我会在这里使用unsafeBitCast吗?我很犹豫地跳入这个话题,因为我已经知道这是极其危险的" ...
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"...
我意识到我可以通过以下方式访问基础内存:
I realize I can access the underlying memory with the following:
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常量后,出于某种原因我需要取消分配吗?
Is that dangerous and would I need to dealloc for some reason after creating the asbd constant?
我已尝试在此发布该帖子 http://sitepoint.com/using-legacy-c-apis-swift ,但我没有得到...在此处寻找最佳做法的任何指导.
I've tried to follow along with the post here http://sitepoint.com/using-legacy-c-apis-swift but I'm just not getting it... Looking for any direction on best practices here.
Doing a bit more research, it seems that it might be possible to create the dictionary using reflect based off this post: http://freecake.angelodipaolo.org/simple-reflection-in-swift/
这是我到目前为止的内容:
Here's what I have so far:
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
等访问描述中的所有值.将其转换为字典是没有意义的,因为那不是真正的字典,没有值的键.
You are doing everything correctly, you can access all the values in the description with asbd.mSampleRate
and such. It wouldn't make sense to convert it to a dictionary because that's just not what it is, there are no keys for the values.
使用这样的指针时,也不必取消任何分配,除非您自己分配(使用malloc
或alloc
时)
You also don't have to dealloc anything when working with pointers like this unless you allocate one yourself (when using malloc
or alloc
)
这篇关于转换Swift UnsafePointer< AudioStreamBasicDescription>到字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!