【问题标题】:Alamofire Value of type 'Result<Any, AFError>' has no member 'isSuccess' (Swift 5)'Result<Any, AFError>' 类型的 Alamofire 值没有成员 'isSuccess' (Swift 5)
【发布时间】:2019-10-07 10:52:08
【问题描述】:

我有一个简单的程序,在我的早期版本 (Swift 4) 中使用过。我现在已经更新到新版本(Swift 5,Alamofire 5.0.0-rc.2),因为我还是初学者,我遇到了这个问题。我已经可以重写一些代码部分。

let headers: HTTPHeaders = [
        "SubscriptionKey": SubscriptionKey,
        "Host": HostName
    ]

    AF.request(URL(string: userInfoUrl)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers)
        .validate()
        .responseJSON { response in
            guard response.result.isSuccess else {
                log.error("Error requesting user ID: \(String(describing: response.result.error))")
                completion(false, nil)
                return
            }

            guard let json = response.result.value as? [String: Any], let userId = json["userId"] as? String else {
                log.error("Malformed result from API call.")
                completion(false, nil)
                return
            }

response.result.isSuccess 是不可能的。如何重复使用这部分?

错误:“Result”类型的值没有成员“isSuccess”

作为一个可能的解决方案,我将测试上述解决方案。

AF.request(URL(string: userInfoUrl)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers)
    .validate()
    .responseJSON { response in
        switch response.result{
        case .success(let value):
            //succcess, do anything

        case .failure(let error):
            log.error("Error requesting user ID: \(String(describing: response.error))")
            completion(false, nil)
            return
        }

【问题讨论】:

标签: ios swift alamofire


【解决方案1】:

结果包含.success.failure 大小写。

所以你应该这样对待isSuccess

switch response.result {
   case .success(let value):
//success, do anything
   case .failure(let error): 
//failure
}

【讨论】:

  • 感谢您的提示。我已经尝试过这种方式,但我无法定义我的成功案例。通过 guard response.result.isSuccess 查看我的代码
  • 成功案例实际上是case .success。如果你想要它,就像你拥有的那样,它应该是 guard try? response.result.get() != nil else { return } 。澄清一下:您使用的是 Alamofire 5.0.0 rc,它现在支持 Swift 5 Result 类型,因此旧文档参考是错误的。您可以在此处查看快速结果类型developer.apple.com/documentation/swift/result
  • 从 AlamoFire 4 升级到 AlamoFire 5 时,人们怎么知道要这样做?是否只是因为 AlamoFire 文档假定每个人都阅读过 developer.apple.com/documentation/swift/result ,并且提供这些正确答案的人已经阅读了特定的 Apple 文档页面?
  • @AndrewKoster 由于主要版本 Alamofire 发布迁移文档。 Alamofire 团队做得很好,但管理依赖项是开发人员的责任。不要责怪开源,他们尽最大努力提升您的体验并进一步发展他们的“产品”github.com/Alamofire/Alamofire/blob/master/Documentation/…
  • @IliyaKisliy 但这是我的观点,Alamofire 项目没有发布解决此问题的迁移文档,至少在我能找到的任何地方都没有。那么人们怎么知道如何解决这个不可避免的依赖问题,它会在你升级到这个版本的 Alamofire 时 100% 地破坏你的代码呢?是否有一个我不知道的秘密 Alamofire 迁移文档网站?
猜你喜欢
  • 2021-06-25
  • 1970-01-01
  • 2022-01-20
  • 2020-04-18
  • 2020-01-01
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多