Swift:传递一个C函数(SCNetworkReachabilityCallBack)

时间:2014-09-04 07:36:20

标签: ios swift reachability

我正在尝试将SCNetworkReachabilitySetCallback的第二个参数称为SCNetworkReachabilityCallBack

在Swiftified SCNetworkReachability.h中,这被定义为:

typealias SCNetworkReachabilityCallBack = CFunctionPointer<((SCNetworkReachability!, SCNetworkReachabilityFlags, UnsafeMutablePointer<Void>) -> Void)>

在Objective-C中,我声明了一个类似的函数:

static void MYReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
    // do something
}

并将其作为参数传递。我是Swift似乎我应该传递一个带有SCNetworkReachabilityCallBack中定义的签名的闭包,类似这样:

let callback = { (target: SCNetworkReachability!, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) in
    // Do something
}

但这会产生错误:

  

无法转换表达式的类型'(SCNetworkReachability!,   SCNetworkReachabilityFlags,UnsafeMutablePointer) - &gt;   (SCNetworkReachability !, SCNetworkReachabilityFlags,   UnsafeMutablePointer) - &gt; $ T0'输入'(SCNetworkReachability!,   SCNetworkReachabilityFlags,UnsafeMutablePointer) - &gt;   (SCNetworkReachability !, SCNetworkReachabilityFlags,   UnsafeMutablePointer) - &gt; $ T0'

有点令人费解!

是否有其他人遇到此问题或有解决方案?

1 个答案:

答案 0 :(得分:-5)

static void MYReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
        // do something
}