swift - swift @objc 和选择器:从选择器返回类型的编译器错误
问题描述
在 MacOS 10.14.6、Swift 和 XCode 11.0、VLCKit 3.3.0 上,我尝试在一个类中使用 @objc 函数创建回调。但是使用#selector 指向我的@objc func,编译器会产生错误
这些是我在课堂上声明的 2 个函数
class VLCStreamProcessor {
// ...
@objc func lock_frame(
opaque: UnsafeMutableRawPointer? ,
planes: UnsafeMutablePointer<UnsafeMutableRawPointer?>?)
-> UnsafeMutableRawPointer?
{ // ...
return UnsafeMutableRawPointer(user_data.pic)
}
@objc func unlock_frame(
opaque: UnsafeMutableRawPointer?,
picture: UnsafeMutableRawPointer?,
planes: UnsafePointer<UnsafeMutableRawPointer?>?)
{ // ...
}
然后在另一个函数(myVLCProcessing)中,仍然在同一个类中,我准备我的回调
func myVLCProcessing()
{ // ...
libvlc_video_set_callbacks(
mplayerPtr, // type libvlc_media_player_t*
#selector( lock_frame(opaque:planes:) ), // type libvlc_video_lock_cb <- 1st error
#selector( unlock_frame(opaque:picture:planes: )), // type libvlc_video_unlock_cb <- 2nd error
0, // type libvlc_video_display_cb
opaque // type void*
)
// ...
}
在那里我得到一个编译器错误,在 #selector 的 2 行上:首先:
Cannot convert value of type 'Selector' to expected argument type 'libvlc_video_lock_cb?' (aka 'Optional<@convention(c)(Optional<UnsafeMutableRawPointer>, Optional<UnsafeMutablePointer<Optional<UnsafeMutableRawPointer>>>) -> Optional<UnsafeMutableRawPointer>>')
第二:
Cannot convert value of type 'Selector' to specified type 'libvlc_video_unlock_cb' (aka '@convention(c) (Optional<UnsafeMutableRawPointer>, Optional<UnsafeMutableRawPointer>, Optional<UnsafePointer<Optional<UnsafeMutableRawPointer>>>) -> ()')
从 libvlc (libvlc_media_player.h) 中,2 个 C 函数预计为:
typedef void *(*libvlc_video_lock_cb)(void *opaque, void **planes);
typedef void (*libvlc_video_unlock_cb)(void *opaque, void *picture,
void *const *planes);
欢迎任何建议。
解决方案
只有全局函数或闭包(不捕获上下文)可以传递给回调参数的 C 函数。与如何使用实例方法作为仅采用 func 或文字闭包的函数的回调类似,您可以通过将其转换为指针并返回来“隧道” self
(指向实例的指针)到回调函数。void
然后回调函数可以调用实例方法:
class VLCStreamProcessor {
func lock_frame(
planes: UnsafeMutablePointer<UnsafeMutableRawPointer?>?)
-> UnsafeMutableRawPointer?
{ // ...
}
func unlock_frame(
picture: UnsafeMutableRawPointer?,
planes: UnsafePointer<UnsafeMutableRawPointer?>?)
{ // ...
}
func myVLCProcessing() {
// ...
let opaque = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
libvlc_video_set_callbacks(mplayerPtr, { (opaque, planes) -> UnsafeMutableRawPointer? in
let mySelf = Unmanaged<VLCStreamProcessor>.fromOpaque(opaque!).takeUnretainedValue()
return mySelf.lock_frame(planes: planes)
}, { (opaque, picture, planes) in
let mySelf = Unmanaged<VLCStreamProcessor>.fromOpaque(opaque!).takeUnretainedValue()
mySelf.unlock_frame(picture: picture, planes: planes)
}, nil, opaque)
}
}
这里假设VLCStreamProcessor
只要设置了 libvlc 回调就存在,否则必须保留指针。
推荐阅读
- centos - elrepo 存档元数据未列出截至 2018 年 10 月 1 日 0600 UTC 的 kernel-lt
- java - Android:使搜索活动成为单一顶部
- android - 检索错误的负适配器位置
- r - 如何在 R 中使用排名函数(使用 dplyr)
- api - HERE API 请求返回视图而不是 JSON
- amazon-web-services - 在 AWS EC2 上使用 mup 部署 VulcanJS 应用程序
- c# - c#方法没有按照我写的顺序运行
- javascript - 将大数转换为以十进制形式表示的大基数
- c# - 如何使用通用约束进行这种棘手的向下转换?
- jquery - 全日历更改资源事件取决于视图