Swift3 Swift 3在NSOperation中调用init时添加@esaping属性会崩溃

Swift3 Swift 3在NSOperation中调用init时添加@esaping属性会崩溃,swift3,ios10,Swift3,Ios10,我有一个函数定义了一个闭包,如下所示: func synchronizeData(completion externalCompletion: RequestsCompletionHandler?) { let closure = { (operationCompletion:@escaping ()->Void) in assert(Thread.isMainThread, "must be the main thread")

我有一个函数定义了一个闭包,如下所示:

func synchronizeData(completion  externalCompletion: RequestsCompletionHandler?) {

    let closure = {

        (operationCompletion:@escaping ()->Void) in

        assert(Thread.isMainThread, "must be the main thread")

        /*
        Internal (non-optional) completion handler
        */
        let internalCompletion: RequestsCompletionHandler = {

            (success, newData, decommissionRequired, errors) -> Void in

            /*
            Synchronization is finished. Firstly, call either the external completion handler or the delegate
            */
            if let externalCompletion = externalCompletion {

                externalCompletion(success, newData, decommissionRequired, errors)
            }
            else {

                self.delegate?.synchroniationInteractor(self, didSynchronizeDataWithStatus: success, dataIsNew: newData, decommissionRequired: decommissionRequired, error: errors.last)
            }

            /*
            Now call the closure operation's completion handler
            */
            operationCompletion()
        }

        /*
        The synchronization itself
        */
        guard let _ = self.keychain.retrieveActivationIdentifiers() else {

            internalCompletion(false, false, false, [NSError(domain: "", code: 0, userInfo: ["reason" : "unable to retrieve credentials"])])
            return
        }

        var errors :[NSError] = []

        var numberOfRequests = 0
        var completedRequests = 0

        var decommissionRequired: Bool?

        /*
        Synchronization results handler. Regardless of success for fail, increase completed requests counter and append any errors to array, if final request call main completion block
        */
        let handleCompletedRequests = {

            (error: NSError?) -> Void in

//          assert(NSThread.isMainThread(), "must be the main thread")

            if let error = error {
                errors.append(error)
            }

            completedRequests += 1

            if(completedRequests >= numberOfRequests) {

                internalCompletion(errors.count == 0, true, decommissionRequired, errors)

                /*
                Decrement operations counter
                */
                self.manageBusy(retain: false)
            }
        }

        /*
        Increment operations counter
        */
        self.manageBusy(retain: true)

        /*
        Do the actual synchronization.
        Fetch the Patient Data first
        */
        self.fetchPatientDataInternal {

            (success, newData, error) -> Void in

            numberOfRequests = 6

            //Fetch Patient Schedule
            self.fetchPatientScheduleInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            //Fetch Patient Thresholds
            self.fetchPatientThresholdInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            //Fetch Patient Device Settings
            self.fetchPatientDeviceSettingsInternal {

                (success, newData, decommissionReq, error) -> Void in

                decommissionRequired = decommissionReq
                handleCompletedRequests(error)
            }

            // Device Checkin
            self.deviceCheckInInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            // Upload Vitals
            self.uploadPendingVitalsInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }

            //Upload Health sessions
            self.uploadPendingHealthSessionsInternal {

                (success, newData, error) -> Void in
                handleCompletedRequests(error)
            }
        }
    }
    let operation =  CIAsyncronousOperation.init(closure: closure as! (()->Void)-> Void)
    operation.name = "Data Synchronization"
    isolationQueue.addOperation(operation)
}
当我们在上述函数中调用这一行时,即

let operation =  CIAsyncronousOperation.init(closure: closure as! (()->Void)-> Void)
应用程序因以下信息而崩溃:

0x00000001003b083c cApplication`部分应用cApplication.SynchronizationInteractor的转发器。(synchronizeData(完成:Swift.Optional()>)->())(在SynchronizationInteractor.Swift处关闭#1)

CIAsyncronousOperation init的定义如下:

init(closure aClosure: @escaping (()->Void)-> Void)
{

        closure = aClosure
}

我无法找出坠机的原因。是由于强制转换问题还是由于新的Swift 3语法更改?

如果您必须强制转换函数,则很有可能您的函数签名不正确。如果CIAsyncronousOperation定义如下,则它将编译。函数和作为参数aClosure的函数都需要设置为@escaping

class CIAsyncronousOperation {
    init(closure aClosure: @escaping (@escaping ()->Void)->Void)
    {
        closure = aClosure
    }
    var closure : (@escaping ()->Void)-> Void;
}

谢谢我还是抓不住“逃跑”。请允许我花更多的时间来理解它。如果可以在函数返回后调用闭包,也就是说,如果它退出函数,则需要将闭包设置为@escaping。