Xcode:Alamofire获取字符串响应 [英] Xcode : Alamofire get String response

查看:132
本文介绍了Xcode:Alamofire获取字符串响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试登录...
,只要凭据正确无误, .php文件返回一个 json ,我可以通过以下代码从 Alamofire 中获取该json:

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

    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

        }
    }

现在...每当凭据错误时,.php都不会给出json ..而是返回字符串..例如 wrong_password或 userLocked等...
我如何获得字符串响应通过Alamofire吗?

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?

推荐答案

如果您想要 JSON 响应,请使用 .responseJSON ,如果您想要 String 响应,请使用 .responseString 。如果两者都使用。希望得到帮助。

If you want JSON response use .responseJSON , if you want String response use .responseString. If you want both use both. Hope this help.

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     
        }
    }

这篇关于Xcode:Alamofire获取字符串响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆