在 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);
欢迎任何建议。