Xcode:Alamofire 获取字符串响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44095161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:59:36  来源:igfitidea点击:

Xcode : Alamofire get String response

xcodealamofire

提问by Sam

I am new in IOS development and currently learning networking with Alamofire

我是 IOS 开发新手,目前正在学习 Alamofire 网络

i am trying to make a login ... whenever the credentials are correct the .php file returns a jsonand i am able to get that json from Alamofirethrough the following code:

我正在尝试登录...只要凭据正确,.php 文件就会返回 a json,我可以Alamofire通过以下代码获取该 json :

    Alamofire.request(loginUrl, method: .post, parameters: parameters).responseJSON { (response:DataResponse<Any>) in
        print("String:\(response.result.value)")
        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(self.loginUrl)
                print(data)
            }

        case .failure(_):

            print(self.loginUrl)
            print("failed")
            print("Error message:\(response.result.error)")
            break

        }
    }

now...when ever the credentials are wrong, the .php does't give the json..instead it return a string ..for example "wrong_password" or "userLocked" etc etc... how can i get the String response through Alamofire?

现在......当凭据错误时,.php 不会提供 json..而是返回一个字符串..例如“wrong_password”或“userLocked”等......我如何获得字符串响应通过阿拉莫火?

回答by Arafin Russell

If you want JSONresponse use .responseJSON, if you want Stringresponse use .responseString. If you want both use both. Hope this help.

如果你想要JSON响应使用.responseJSON,如果你想要字符串响应使用.responseString。如果你想要两者都使用。希望这有帮助。

Alamofire.request(loginUrl, method: .post, parameters: parameters)
     .responseJSON { response in
       print("JSON:\(response.result.value)")
       switch(response.result) {
       case .success(_):
          if let data = response.result.value{
             print(data)
           }

        case .failure(_):

            print("Error message:\(response.result.error)")
            break

        }
    }
     .responseString { response in
       print("String:\(response.result.value)")
       switch(response.result) {
       case .success(_):
          if let data = response.result.value{
             print(data)
            }

       case .failure(_):
           print("Error message:\(response.result.error)")
           break     
        }
    }

回答by Firda Sahidi

I resolve this by:

我通过以下方式解决此问题:

print(response.request)  // original URL request
print(response.response) // URL response
print(response.data)     // server data
print(response.result)   // result of response serialization

source: https://github.com/Alamofire/Alamofire/issues/818

来源:https: //github.com/Alamofire/Alamofire/issues/818