I checked the similar question, but the issue occurs in my case is totally different.
I am using typealias
to avoid rewriting similar completion block declaration.
typealias FetchFilesCompletionBlock = ( _ files: OPFiles?, _ error: Error?) -> Void
In the function definition, I am using the optional type of FetchFilesCompletionBlock
. Even though, the function is called with a completion block, in function body onCompletion becomes nil.
func fetchFile(_ param: [String: String]? = nil, onCompletion: FetchFilesCompletionBlock?) {
// I found onCompletion is nil here.
// function body
}
That fetchFile(_: onCompletion:)
is called as follows:
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)
If I remove the as? FetchFilesCompletionBlock
from the above snippet, I got a compile-time error Cannot convert value of type '(OPFiles?, NSError?) -> ()' to expected argument type 'FetchFilesCompletionBlock?'
.