尝试转换为swift 3时出现OpaquePointer错误

时间:2016-10-07 15:45:41

标签: ios swift

我正在尝试将我的项目转换为swift 3并且还有以下错误。我试过做了几件事,但我做不到。

错误

  

"无法转换类型'(OpaquePointer,OpaquePointer,LinphoneRegistrationState,UnsafePointer)的值 - > ()'指定类型' LinphoneCoreRegistrationStateChangedCb' (又名' @convention(c)(可选,可选,_LinphoneRegistrationState,可选>) - >()')

对于此代码

var registrationStateChanged: LinphoneCoreRegistrationStateChangedCb = {
    (lc: OpaquePointer, proxyConfig: OpaquePointer, state: LinphoneRegistrationState, message: UnsafePointer<Int8>) in

    switch state{
    case LinphoneRegistrationNone: /**<Initial state for registrations */
        NSLog("LinphoneRegistrationNone")

    case LinphoneRegistrationProgress:
        NSLog("LinphoneRegistrationProgress")

    case LinphoneRegistrationOk:
        NSLog("LinphoneRegistrationOk")

    case LinphoneRegistrationCleared:
        NSLog("LinphoneRegistrationCleared")

    case LinphoneRegistrationFailed:
        NSLog("LinphoneRegistrationFailed")

    default:
        NSLog("Unkown registration state")
    }
}

错误

  

&#34;无法转换类型&#39;(OpaquePointer,OpaquePointer,LinphoneCallState,_)的值 - &gt; ()&#39;指定类型&#39; LinphoneCoreCallStateChangedCb&#39; (又名&#39; @convention(c)(可选,可选,_LinphoneCallState,可选&gt;) - &gt;()&#39;)&#34;

以下代码

for var callStateChanged: LinphoneCoreCallStateChangedCb = {
    (lc: OpaquePointer, call: OpaquePointer, callSate: LinphoneCallState,  message) in

    switch callSate{
    case LinphoneCallIncomingReceived: /**<This is a new incoming call */
        NSLog("callStateChanged: LinphoneCallIncomingReceived")

        if answerCall{
            ms_usleep(3 * 1000 * 1000); // Wait 3 seconds to pickup
            linphone_core_accept_call(lc, call)
        }

    case LinphoneCallStreamsRunning: /**<The media streams are established and running*/
        NSLog("callStateChanged: LinphoneCallStreamsRunning")

    case LinphoneCallError: /**<The call encountered an error*/
        NSLog("callStateChanged: LinphoneCallError")

    default:
        NSLog("Default call state")
    }}

1 个答案:

答案 0 :(得分:0)

Swift 3已经改变了很多东西,对指针的可空性的处理是其中之一。

SE-0055 Make unsafe pointer nullability explicit using Optional

通过此更改,没有可空性注释的C指针将作为Optionals导入。

在您的情况下,OpaquePointer - &gt; OpaquePointer?UnsafePointer<Int8> - &gt; UnsafePointer<Int8>?

OpaquePointer周围还有其他一些变化,但在您的情况下,它们似乎无关紧要。)

Swift很好地推断出闭包参数的类型(当你的代码写成时)。那么,为什么不首先省略闭包中的所有类型注释呢?

这一行:

(lc: OpaquePointer, proxyConfig: OpaquePointer, state: LinphoneRegistrationState, message: UnsafePointer<Int8>) in

为:

lc, proxyConfig, state, message in

而且:

(lc: OpaquePointer, call: OpaquePointer, callSate: LinphoneCallState,  message) in

为:

lc, call, callSate,  message in