将一个Swift结构体转换为UnsafeMutablePointer< Void> [英] Cast a Swift struct to UnsafeMutablePointer<Void>
问题描述
有一种方法可以将Swift结构体的地址转换为void UnsafeMutablePointer?
我没有成功尝试过:
Is there a way to cast a Swift struct's address to a void UnsafeMutablePointer?
I tried this without success:
struct TheStruct {
var a:Int = 0
}
var myStruct = TheStruct()
var address = UnsafeMutablePointer<Void>(&myStruct)
谢谢!
strong> EDIT:上下文
我实际上试图将学习CoreAudio的第一个例子移植到Swift。
这是我的到目前为止:
the context
I am actually trying to port to Swift the first example in Learning CoreAudio.
This is what I have done until now:
func myAQInputCallback(inUserData:UnsafeMutablePointer<Void>,
inQueue:AudioQueueRef,
inBuffer:AudioQueueBufferRef,
inStartTime:UnsafePointer<AudioTimeStamp>,
inNumPackets:UInt32,
inPacketDesc:UnsafePointer<AudioStreamPacketDescription>)
{ }
struct MyRecorder {
var recordFile: AudioFileID = AudioFileID()
var recordPacket: Int64 = 0
var running: Boolean = 0
}
var queue:AudioQueueRef = AudioQueueRef()
AudioQueueNewInput(&asbd,
myAQInputCallback,
&recorder, // <- this is where I *think* a void pointer is demanded
nil,
nil,
UInt32(0),
&queue)
$ b b
我正在努力留在Swift,但如果这证明是一个问题而不是一个优势,我将最终链接到一个C函数。
I am doing efforts to stay in Swift, but if this turns out to be more a problem than an advantage, I will end up linking to a C function.
编辑:底线
如果您遇到此问题,因为您试图在Swift中使用CoreAudio的AudioQueue ... 别。 (阅读注释的细节)
bottome line
If you came to this question because you are trying to use a CoreAudio's AudioQueue in Swift... don't. (read the comments for details)
推荐答案
据我所知,最简单的方法是:
As far as I know, the shortest way is:
var myStruct = TheStruct()
var address = withUnsafeMutablePointer(&myStruct) {UnsafeMutablePointer<Void>($0)}
但是,为什么你需要这个?如果您希望将其作为参数传递,则可以(并且应该):
But, why you need this? If you want pass it as a parameter, you can (and should):
func foo(arg:UnsafeMutablePointer<Void>) {
//...
}
var myStruct = TheStruct()
foo(&myStruct)
这篇关于将一个Swift结构体转换为UnsafeMutablePointer< Void>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!