是否不能将Nil分配给()->()类型? [英] Nil cannot be assigned to type ()->()?

查看:86
本文介绍了是否不能将Nil分配给()->()类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这是一个类中的函数,使您可以双击和单击手势.在Swift 2.3中可以正常工作,但是在转换为Swift 3后会引发一些错误.我无法理解/理解我的一生.我评论了它们的发生位置.

So this is a function from a class that lets you have a double tap and a single tap gesture. It's working fine in Swift 2.3, but after converting to Swift 3 it's throwing a few errors. I can't understand/figure out for the life of me. I commented where they occur.

//  UIShortTapGestureRecognizer.swift
//
//

import Foundation
import UIKit


    func delayHelper(_ time:TimeInterval, task:@escaping ()->()) ->  DelayTask? {

        func dispatch_later(_ block:@escaping ()->()) {
            DispatchQueue.main.asyncAfter(
                deadline: DispatchTime.now() + Double(Int64(time * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC),
                execute: block)
        }

        var closure: ()->()? = task

        var result: DelayTask?

        let delayedClosure: DelayTask = {
            cancel in

//Initializer for conditional binding must have Optional type, not '() -> ()?'
            if let internalClosure = closure {
                if (cancel == false) {
                    DispatchQueue.main.async(execute: internalClosure)
                }
            }

// here it says Nil cannot be assigned to type '() -> ()?'
            closure = nil

            result = nil
        }

        result = delayedClosure

        dispatch_later {
            if let delayedClosure = result {
                delayedClosure(false)
            }
        }

        return result;
    }

    func cancel(_ task:DelayTask?) {
        task?(true)
    }
}

推荐答案

尝试使用:var closure: (()->())? = task

这篇关于是否不能将Nil分配给()->()类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆