Search code examples
swift3alamofirepromisekit

loop result from alamofire service in swift 3


I am working with alamofire and promiseKit and I want to return the result as JSON this is my code:

Services.objServices.ServLogin(auth: auth_dato).then{
            data -> Void in
            print(data)
            SVProgressHUD.dismiss()
            UIApplication.shared.endIgnoringInteractionEvents()
            }.catch{
                error -> Void in
                SVProgressHUD.dismiss()
                UIApplication.shared.endIgnoringInteractionEvents()
            }

func ServLogin(auth: String) -> Promise<String>{
        return Promise<String>{
            fullfil,reject -> Void in
            return Alamofire.request(
                Constants.api_url+"login",
                headers: ["Authorization":"Basic "+auth]).responseString{
                response in
                switch(response.result){
                case .success(let data):
                    fullfil(data)
                case .failure(let error):
                    reject(error)
                }
            }
        }
    }

Now I get this result:

{
  "status": true,
  "data": {
    "usuarios": [
      {
        "id": 1,
        "username": "user",
        "password": "0012",
        "nombres": "Diego",
        "apellidos": "Moreno",
        "estado_user": 1
      }
    ]
  }
}

and I wanna now How can I get the status value and the nombres of user? I was trying this print(data["status"]) but I get this error:

cannot subscript a value of Type "String" with a index of type "String"

thanks in advance


Solution

  • i suggest you use https://github.com/SwiftyJSON/SwiftyJSON Library , it makes parsing json very easy.

    let json = JSON(data)
    let status = json["status"].boolValue