我是iOS的新手,我在应用中有一些Swift代码,该代码根据我从服务器收到的错误消息来切换大小写。邮件已包装好,我很难到达。这是代码
func sendAlamofireRequest(submissionURL: URL, parameters: Parameters, chosenTracker: String) -> String {
var outputMessage = ""
Alamofire.request(submissionURL, method: .post, parameters: parameters, encoding: JSONEncoding.default).validate().responseString() {
response in
switch response.result {
case .success:
print("Validation Successful...\(String(describing: response.value))")
print("response value: \(String(response.value!))")
switch response.value {
case "error_none"?:
outputMessage = "No matching Group Code. If you are having trouble, please go to \nhttps://app.phillyscientists.com"
break
case "error_tooManyIDs"?:
outputMessage = "Error, please contact developer."
break
case "error_noGroupIDReceived"?:
outputMessage = "Try Again."
break
default:
let JSONResponse : JSON = JSON.init(parseJSON: response.result.value!)
//uncomment this section for debugging
// print("=================<JSON RESP>=================");
// print(JSONResponse)
// print("=================</JSON RESP/>=================");
//
let teacherNameGot = self.parseJSONData(json: JSONResponse, trackerValuePassed: chosenTracker)
self.saveJSONDataToUserDefaults(teacher: teacherNameGot)
// outputMessage = "Logged In Successfully!"
break
}
case .failure(let error):
outputMessage = String(error.localizedDescription)
print(outputMessage)
}
}
return outputMessage
}
这是控制台的输出:
Validation Successful...Optional("{\"Error\":\"error_none\"}")
response value: Optional("{\"Error\":\"error_none\"}")
如何获得该值,以便开关盒真正开始工作? 谢谢!
答案 0 :(得分:0)
您可以使用Alamofire方法.responseJSON
,该方法将为您提供一个http对象,该对象具有多个属性,例如request
和response
。以下面的代码为例:
Alamofire.request("https://your-service-url.com/api", method: .post, parameters: paremeters, encoding: JSONEncoding.default, headers: headers).responseJSON{ (data) in
guard let statusCode = data.response?.statusCode, statusCode == 200, let result = data.result.value as? [[String: Any]] else{
print("Error with HTTP status \(data.response?.statusCode ?? 0)")
return
}
var events : [Event] = []
result.forEach({ (rawEvent) in
events.append(Event(from: rawEvent))
})
handler(events, statusCode)
}
请注意,我如何在其中使用.responseJSON
提供的对象,以及如何通过访问data.result.value
从服务中获取结果数组(也就是说,这将取决于服务的数据结构响应)
答案 1 :(得分:0)
输出似乎在Json中,因此我需要将其映射到一个对象(或类,无论您叫什么)。如果您认为它不值得,则可以将其转换为字典,我认为它也可以使用
答案 2 :(得分:0)
@Gustavo caiafa是正确的,将其映射到JSON可以解决问题。这是任何陷入类似困境的人的代码:
Alamofire.request(submissionURL, method: .post, parameters: parameters, encoding: JSONEncoding.default).validate().responseString() { (response) in
let outputResponseJSON : JSON = JSON.init(parseJSON: response.result.value!)
let outputResponseText = JSON(outputResponseJSON)["Error"].stringValue
switch response.result {
case .success:
print("Validation Successful...\(String(describing: response.value))")
print("response value: \(String(describing: String(outputResponseText)))")
switch outputResponseText {
case "error_none":
outputMessage = "No matching Group Code. If you are having trouble, please go to \nhttps://app.phillyscientists.com"
print(outputMessage)
break
case "error_tooManyIDs":
outputMessage = "Error, please contact developer."
print(outputMessage)
break
case "error_noGroupIDReceived":
outputMessage = "Try Again."
print(outputMessage)
break
default:
let JSONResponse : JSON = JSON.init(parseJSON: response.result.value!)
答案是使用SwiftyJSON作为outputResponseJSON和outputResponseText值。