本文介绍了闭包使用非转义参数-Swift 3问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Swift 3中的更改,其中@nonescaping是闭包的默认行为.

I know about the changes in Swift 3 where @nonescaping is default behaviour for closures.

关于更改,我已经成功更改了很多代码,但是我的代码中有一部分我无法摆脱关闭使用非转义参数可能会使其逃脱 >编译错误.

I have successfully changed much of my code regarding the changes but I have one part of my code where I am not able to get rid of the Closure use of non-escaping parameter may allow it to escape compilation error.

我尝试将@escaping添加到updateHandler参数和UpdatedInProgressHandler类型别名中,但这似乎还不够.

I have tried adding @escaping to both the updateHandler parameter and the UpdatedInProgressHandler typealias but it does not seem to be enough.

有人能帮助我弄清楚我的问题的原因吗?

Anyone able to help me figuring out the reason for my problem?

定义类型并定义函数的代码:

The code where typealiases and the function is defined:

// Typealiases used to clean up closures
typealias UpdateInProgressCompletion = () -> ()
typealias UpdateInProgressCancelCompletion = () -> ()
typealias UpdateInProgressHandler = ((_ completed: @escaping UpdateInProgressCompletion) -> ()) -> ()

// Method for wrapping the presentation and dismissal of the custom alert controller
func presentUpdateInProgress(_ taskIdentifier: String?, alertMessage: String?, alertHeader: String? = nil, updateHandler: @escaping UpdateInProgressHandler, cancel cancelHandler: UpdateInProgressCancelCompletion? = nil) {

    let updateInProgressAlert = self.updateInProgressAlert( taskIdentifier, alertMessage: alertMessage, alertHeader: alertHeader ) { action in
        cancelHandler?()
        Logger.debug("User cancelled update")
    }

    updateInProgressAlert.present(completion: nil)

    updateHandler { (completion) in
        updateInProgressAlert.dismiss(completion: completion)
    }
}

调用presentUpdateInProgress函数时,出现关闭使用非转义参数"updateCompleted"的代码可能允许是否转义"的错误.

The code where I get the "Closure use of non-escaping parameter "updateCompleted" may allow if to escape" error when calling presentUpdateInProgress function.

    self.presentUpdateInProgress(taskIdentifier, alertMessage: "My alert message", updateHandler: { (updateCompleted) -> () in

        let task = CreateModelTask(completionHandler: { (resultObject) -> () in
            updateCompleted { // this generates the error

                //Do some stuff with received result
            }
        })

        task.taskIdentifier = taskIdentifier
        SyncManager.sharedManager.addTaskToQueue(task)
    })

推荐答案

updateCompleted的类型为(_ completed: @escaping UpdateInProgressCompletion) -> (),它是函数参数本身,表示它不被转义默认值(请注意,默认情况下不转义"行为仅适用于函数关闭参数,请参见,以及).

updateCompleted is of type (_ completed: @escaping UpdateInProgressCompletion) -> (), which as it's a function parameter itself, means that it is non-escaping by default (note that 'non-escaping by default' behaviour is only applicable to function closure arguments, see this Q&A, as well as its dupe target on the topic).

因此,为了允许updateCompleted转义,您需要在typealias中将(_ completed: @escaping UpdateInProgressCompletion) -> ()标记为@escaping:

Therefore in order to allow updateCompleted to escape, you need to mark (_ completed: @escaping UpdateInProgressCompletion) -> () as @escaping in your typealias:

typealias UpdateInProgressHandler = (@escaping (_ completed: @escaping UpdateInProgressCompletion) -> ()) -> ()

这篇关于闭包使用非转义参数-Swift 3问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!