我正在尝试用AlamoFire 5.4.3、SwiftyJSON 5.0.1更新Xcode 13.2中的一个应用程序。除了以下错误外,我能够使一切正常工作。("Result<Any,AFError>"类型的值没有成员"Value"(
我对斯威夫特很陌生,很想学习。该应用程序在使用旧版本的AlamoFire时运行良好。这个应用程序不是我最初写的。如有任何帮助,我们将不胜感激。如果我能澄清任何事情,请告诉我。
James
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
}
}
}
这个错误很容易解释。您正在接收一个结果枚举。它包含成功或失败。你忽略了他们两个。你需要的是获得它们的相关值:
获得成功value
:
case let .success(value):
获取故障error
:
case let .failure(error):