2017-06-19 221 views
0

我有這樣定義的關閉,無法將類型'(_) - >()'的值分配給類型'((String,String,String,Int) - >())?'

public var onLogCompletion:((_ printLog:String,_ fileName:String,_ functionName:String,_ lineNumber:Int) ->())? = nil 

這是這樣的更新,

fileprivate func printerCompletion(printLog:String, fileName:String, functionName: String, lineNumber:Int) -> Void { 
    if onLogCompletion != nil { 
     onLogCompletion!(printLog, getFileName(name: fileName), functionName, lineNumber) 
    } 
} 

,並用它這樣的,

Printer.log.onLogCompletion = { (log) in 
     //print(log) 
     //print(log.0) 
    } 

錯誤:

Cannot assign value of type '(_) ->()' to type '((String, String, String, Int) ->())?'

但是這給了我上面的錯誤,不知道該怎麼辦?

同樣的工作正常與Swift 3.x.

+0

有人,請告訴我downvote我的問題的原因是什麼?這與之前提出的完全不同。 – Hemang

回答

1

它在Swift 4中不工作的原因是因爲Distinguish between single-tuple and multiple-argument function types(SE-0110)

如果你仍然想以你在Swift 3中做的方式工作,那麼你需要像這樣設置函數類型的參數列表,使用Double parentheses

public var onLogCompletion:(((String,String,String,Int)) ->())? = nil 

現在你們都去

Printer.log.onLogCompletion = { (log) in 
    //print(log) 
    //print(log.0) 
} 
+1

太棒了!好找Nirav D. – Hemang

+0

@Hemang歡迎隊友:)很高興爲你效勞:) –

相關問題