【问题标题】:Swift4 Error Cannot convert value of type '(_) -> Void' to expected argument type '(_) -> _'Swift4 错误无法将类型“(_)-> Void”的值转换为预期的参数类型“(_)-> _”
【发布时间】:2023-04-04 05:48:02
【问题描述】:

在 Swift 4 中使用 PromiseKit 进行 API 调用:

 let apiCall = ApiManager.shared.fetchActors()
    apiCall.then { actors -> Void in
        self.dataSourceArray = actors
        self.tableView.reloadData()
        }.catch { error -> Void in

        }

我收到此错误:

无法将类型 '() -> Void' 的值转换为预期的参数类型 '() -> _'

我该如何解决这个问题?

【问题讨论】:

标签: swift swift4 promisekit


【解决方案1】:

使用 .done 而不是使用 .then 它会起作用。

【讨论】:

    【解决方案2】:

    解决方案:

    func fetchActorsFromApi () -> Promise<[Actor]> {
        return Promise<[Actor]> { seal in
            return Alamofire.request(API_URL).validate().responseString(completionHandler: {
                response in
    
                switch (response.result) {
                case .success(let responseString1):
                    print (responseString1) // should be converted into the modelclass
                    let actorResponse = ActorApiResponse(JSONString: "\(responseString1)")!
                    seal.fulfill(actorResponse.actors!)
                case .failure(let error):
                    print (error)
                    seal.reject(error)
                }
            })
        }
    }
    

    【讨论】:

    • 如果我需要处理通用“模型类”/ 而不仅仅是“演员”模型类,语法是什么。
    【解决方案3】:
    let apiCall = ApiManager.shared.fetchActorFromAPI()
            let _ = apiCall.done {
                actors -> Void in
                self.dataSource = actors
                self.myTableView.reloadData()
                }.catch {
                    error -> Void in
            }
    

    使用这个。在新版本的 PromiseKit .done() 函数可以做到这一点。文档:https://github.com/mxcl/PromiseKit

    【讨论】:

      【解决方案4】:

      .catch { error -&gt; Void in 替换为.catch { error in

      【讨论】:

      • actors 呢?
      • 我猜同样的事情。删除-&gt; Void
      • 在演员部分,它给了我错误:无法推断复杂的闭包返回类型;添加显式类型以消除歧义。
      猜你喜欢
      • 1970-01-01
      • 2023-02-05
      • 2017-01-13
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多