2017-04-22 69 views
2

所以這是一個類,讓你有一個雙擊和一個輕拍手勢功能。它在Swift 2.3中工作正常,但在轉換爲Swift 3之後,它拋出了一些錯誤。我無法理解/理解我的生活。我評論他們在哪裏發生。無法分配到type() - >()?

// 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) 
    } 
} 
+3

這意味着返回參數是可選。試試(() - >())? – totiG

+0

我有些懷疑這會在Swift 2.3中編譯,它也應該考慮將'() - >()?'作爲返回'Void?'的函數。 – Hamish

回答

4

嘗試用:var closure: (()->())? = task

0

From another topic:

func someFunction(someParameter: someType, completionClosure: @escaping @autoclosure() ->()? = nil) 

使用@autoclosure的讓我分配給無可選關閉。

@autoclosure如何幫助我逃脫(爲什麼「我的」關閉是「不好」,但Swift_generated_one_that_simply_wraps_mine是?)的完整細節。 但是,它幫助我擺脫... enter image description here ...並叫我以兩種方式功能:

someFunction(x, { ... }) 

OR

someFunction(x) 
0

這裏就是這樣,我怎麼能克服這一點。這裏鏗鏘是不知道它的類型。我給了一個typealias和可選的聲明,

typealias appDelegateBlock =()->() 

在你的類,只是它聲明爲可選,

var block: appDelegateBlock? 
block = task 
相關問題