【发布时间】:2017-06-21 08:52:36
【问题描述】:
我检查了similar question,但在我的情况下出现的问题完全不同。
我使用typealias 来避免重写类似的完成块声明。
typealias FetchFilesCompletionBlock = ( _ files: OPFiles?, _ error: Error?) -> Void
在函数定义中,我使用了FetchFilesCompletionBlock的可选类型。即使使用完成块调用函数,在函数体中 onCompletion 变为 nil。
func fetchFile(_ param: [String: String]? = nil, onCompletion: FetchFilesCompletionBlock?) {
// I found onCompletion is nil here.
// function body
}
fetchFile(_: onCompletion:) 调用如下:
let onCompletion = {(files, error) in
DispatchQueue.main.async(execute: {[weak self]() in
self?.onCompletion(files, error: error)
})
} as? FetchFilesCompletionBlock
// Here also I found that onCompletion is nil
dataManager.fetchFile(param, onCompletion: onCompletion)
如果我从上面的 sn-p 中删除 as? FetchFilesCompletionBlock,我会得到一个编译时错误 Cannot convert value of type '(OPFiles?, NSError?) -> ()' to expected argument type 'FetchFilesCompletionBlock?'。
【问题讨论】:
标签: ios swift3 completionhandler