问题描述
限时送ChatGPT账号..我正在尝试在 Xcode 中使用 Swift 为 AudioServicesAddSystemSoundCompletion 中的参数创建一个 AudioServicesSystemSoundCompletionProc 实例,但失败了.
I'm trying and failing to create an instance of AudioServicesSystemSoundCompletionProc for an argument in AudioServicesAddSystemSoundCompletion using Swift in Xcode.
这是我目前得到的
func completionCallback(ssID:SystemSoundID,clientData:UnsafeMutablePointer<Void>) -> Void {
}
var foo:(ssID:SystemSoundID,clientData:UnsafeMutablePointer<Void>) -> Void = completionCallback;
AudioServicesAddSystemSoundCompletion(soundID, nil, nil, foo, nil);
我在一些解释如何在 Swift 中编写等效的 C 函数指针的指南的帮助下写了这个,但这会引发这个错误:
I wrote this with the help of some guides explaining how to write equivalent C Function Pointers in Swift, but this throws this error:
'(ssID: SystemSoundID, clientData: UnsafeMutablePointer<Void>) -> Void' is not convertible to 'AudioServicesSystemSoundCompletionProc'
文档显示了 Objective-C 声明:
The documentation shows the Objective-C declaration:
typedef void (*AudioServicesSystemSoundCompletionProc) ( SystemSoundID ssID, void *clientData );
这是使用 Xcode 时显示的声明:
This is declaration shown when using Xcode:
typealias AudioServicesSystemSoundCompletionProc = CFunctionPointer<((SystemSoundID, UnsafeMutablePointer<Void>) -> Void)>
我不确定如何在 Swift 中正确实现 AudioServicesSystemSoundCompletionProc.
I'm not sure how to implement AudioServicesSystemSoundCompletionProc correctly in Swift.
推荐答案
David 的回答是正确的.但澄清一下,AudioServicesSystemSoundCompletionProc 需要在objective-c 中完成,然后桥接到swift.你可以自己编写一个实现,或者......它已经在这里为你完成:https://gist.github/jparishy/7b76edf8d0fcca1d63b0(大卫提到)
David's answer is correct. But just to clarify, AudioServicesSystemSoundCompletionProc needs to be done in objective-c, then bridged across to swift. You can either write an implementation yourself, or... it is already done for you here: https://gist.github/jparishy/7b76edf8d0fcca1d63b0 (as mentioned by David)
您需要转到该链接,下载文件 FunctionPointer.h 和 FunctionPointer.c,将其放入您的项目文件夹中,然后使用桥接头将其链接到 swift.
You need to go to that link, download the files FunctionPointer.h and FunctionPointer.c, put it in your project folder, then link it to swift using bridging header.
这样做:
转到您项目的构建设置,向下滚动到显示 Objective-C Bridging Header 的位置,将内容编辑为YourProject/Bridging-Header.h"(其中 YourProject 是您的项目名称)在您的项目中创建一个objective-c 头文件,并将其命名为Bridging-Header.h在头文件中,添加#import "FunctionPointer.h"现在你可以在你的 swift 项目中的任何地方访问 FunctionPointer ......甜蜜
Now you can access FunctionPointer anywhere inside your swift project... sweet
回到完成处理程序,在您想要使用 AudioServicesAddSystemSoundCompletion 的代码中,执行如下操作:
Back to the compeletion handler, in your code, where you want to use AudioServicesAddSystemSoundCompletion, do something like this:
// at the top of the class
var functionPointer: AudioServicesCompletionFunctionPointer?
// in the code
var me = self
let userData = withUnsafePointer(&me) { ptr in
return unsafeBitCast(ptr, UnsafeMutablePointer<Void>.self)
}
self.functionPointer = AudioServicesCompletionFunctionPointer(systemSoundID: soundId, block: {(systemSoundID: SystemSoundID, userData: UnsafeMutablePointer<Void>) -> () in
// sound has ended, do your stuff here
}, userData: userData)
AudioServicesAddSystemSoundCompletion(soundId, CFRunLoopGetMain(), kCFRunLoopDefaultMode, AudioServicesCompletionFunctionPointerpletionHandler(), userData)
您必须将 NSObject 更改为 TheClassYoureUsingThisIn 并将 soundId 更改为您的 soundId
where you'll have to change NSObject to TheClassYou'reUsingThisIn and soundId to your soundId
这篇关于如何在 Swift 中实现 AudioServicesSystemSoundCompletionProc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论