【问题标题】:Value of type 'Result<Any, AFError>' has no member 'value' (with Xcode 13.2 and AlamoFire 5.4.3)'Result<Any, AFError>' 类型的值没有成员 'value'(使用 Xcode 13.2 和 AlamoFire 5.4.3)
【发布时间】:2022-01-20 01:46:48
【问题描述】:

我正在尝试使用 AlamoFire 5.4.3、SwiftyJSON 5.0.1 更新 Xcode 13.2 中的应用程序。除了以下错误之外,我能够让一切正常工作。 ('Result' 类型的值没有成员'value')

我对 Swift 很陌生,并且渴望学习。该应用程序在使用旧版本的 AlamoFire 时运行良好。我最初没有编写这个应用程序。任何帮助将不胜感激。如果我能解决任何问题,请告诉我。

詹姆斯

class func getPatiens(options: String, completion: @escaping (_ status: Bool, _ message:String, _ patientsList: [PatientEntity]) -> Void) {
        
        let url = Common.getRequestURL(action: "PATIENTS", options: options, index: "")
        
        AF.request(url, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
            switch(response.result) {
            
            case .success(_):

               // ERROR: Value of type 'Result<Any, AFError>' has no member 'value'
               if response.result.value != nil{

                // ERROR: Value of type 'Result<Any, AFError>' has no member 'value'
                let jsonResult = JSON(response.result.value as! [String: Any])
                    
                    let patientsList = jsonResult["results"].array!
                    
                    var arrPatients = [PatientEntity]()
                    
                    for index in 0 ..< patientsList.count {
                        let patient = PatientEntity()
                        
                        patient.p_dol  = patientsList[index]["DOL"].string!
                        patient.p_id   = patientsList[index]["ID"].string!
                        patient.p_name = patientsList[index]["NAME"].string!
                        
                        arrPatients.append(patient)
                    }
                    
                    completion(true, "success", arrPatients)
                }
                break
            
            case .failure(_):
                completion(false, "Server Failed", [])
                break
            }
        }
    }

【问题讨论】:

  • 摆脱 SwiftlyJSON 并开始使用 Codable
  • Result 定义在哪个框架中?
  • @AdamPro13 Result 是原生的generic enumeration Swift 类型
  • @LeoDabus 感谢您提供有关 SwiftyJSON 的信息。该应用程序是几年前由签约开发人员编写的。我正在尝试通过一些更改来查看是否可以启动并运行它。
  • 看起来这段代码最初使用的是旧版本的 Alamofire,它定义了自己的 Result 类型。该类型确实具有 value 属性,因此最好扩展 Swift 的 Result 并重新创建该属性。

标签: ios swift alamofire


【解决方案1】:

这个错误是不言自明的。您正在收到一个结果枚举。它包含一个成功或失败。你忽略了他们两个。您需要的是获取它们的关联值:

获得成功value:

case let .success(value):

获取失败error:

case let .failure(error):

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多