我在用 Objective C 编写的框架中有一个完成处理程序...
这是 block 类型的 typedef。它需要一组 PHErrors。
typedef void (^PHBridgeSendErrorArrayCompletionHandler)(NSArray *errors);
当我尝试在 Swift 中使用它时,我正在做......
anObject.aMethod(completionHandler: { (errors: [ AnyObject?]) -> () in
...rest of code
}
但我一直收到这个错误:
Cannot convert value of type '([AnyObject?]) -> ()' to expected argument type 'PHBridgeSendErrorArrayCompletionHandler!'
谁能帮忙,我很困惑,看起来它应该对我有用。
最佳答案
或者更好的是,您仍然可以将 typedef
用作 typealias
。
定义
typealias PHBridgeSendErrorArrayCompletionHandler = (_ errors: [Error]?) -> Void
实现
func myFunctionWithErrorCompletion(completion: PHBridgeSendErrorArrayCompletionHandler) {
// Define empty array to add errors to
var errors:[Error]?
// Do Your Logic that may store errors to array
// Completion and pass errors
completion(errors)
}
用法
func anotherOfMyFunctions() {
// Call the function
myFunctionWithErrorCompletion { (errors) in
if let completionErrors = errors {
// React to errors
}
}
}
关于objective-c - Swift 中使用的 Objective C Array completionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40655952/