【问题标题】:Store completion handler and call success/error later存储完成处理程序并稍后调用成功/错误
【发布时间】:2020-06-30 08:02:58
【问题描述】:

我在下面的班级中有一个协议/功能,

func getMovieList(completionHandler: @escaping (Result<[String], Error>) -> Void) { }

当调用上述方法时,我想存储完成处理程序,并在后面调用成功/错误。

我尝试创建一个像下面这样的类型别名,

typealias AlbumListCompletionHandler = (((Result<[String], Error>)) -> Void)?

在我的课堂上,

var completionHandlerObj: AlbumListCompletionHandler

func getMovieList(completionHandler: @escaping (Result<[String], Error>) -> Void) { 
    completionHandlerObj = completionHandler
    /...
    .../
}

但我想知道如何调用completionHandlerObj 中的成功/错误块,这有点令人震惊。谁能帮我解决这个问题?

【问题讨论】:

    标签: ios swift swift5 completionhandler


    【解决方案1】:

    它应该像这样工作

    completionHandlerObj(.success(["",""]))
    completionHandlerObj(.failure(ErrorObject))
    

    【讨论】:

    • 该死,我也在尝试。但是我的 Xcode 很疯狂,现在它可以工作了。
    • @Perseus 我很高兴它现在可以工作了。请接受答案,也许这对其他人也有帮助。
    猜你喜欢
    • 2023-03-16
    • 2013-11-11
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    相关资源
    最近更新 更多